好吧,所以我正在做一项任务,我感到很沮丧。任务要求我向用户询问一个数字,然后说这个数字是偶数还是奇数,但是如果用户输入“完成”,程序就会退出。
所以我的问题是,如何同时检查字符/int的输入,然后决定它是哪一个。
// ConsoleApplication2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
bool isEven(int userAnswer);
using namespace std;
int userAnswer;
int main()
{
cout << "Which number would you like to check?" << endl;
cin >> userAnswer;
if (isEven(userAnswer) == false)
{
cout << userAnswer << " is a odd number." << endl;
}
else if (isEven(userAnswer) == true)
{
cout << userAnswer << " is a even number." << endl;
}
cin.get();
cin.get();
return 0;
}
bool isEven(int userAnswer)
{
if (userAnswer % 2 == 0)
{
return true;
}
else
{
return false;
}
}
发布于 2015-10-06 14:41:58
读入字符串(在这两种情况下都有效),然后自己解析字符串。
发布于 2015-10-06 14:50:29
读入std::string
,如果done
在字符串中,则退出。否则,转换为int
并按原样继续进行。提示:见std::stoi
https://stackoverflow.com/questions/32972801
复制相似问题