c++ - 检查 Char/Int

标签 c++ loops if-statement while-loop evaluation

好吧,我正在做作业,我很沮丧。作业要我向用户询问一个数字,然后说这个数字是偶数还是奇数,但如果用户键入“完成”,程序将退出。

所以我的问题是你如何同时检查字符/整数的输入,然后决定它是哪个。

// 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;
    }
}

最佳答案

读入一个字符串(在这两种情况下都有效),然后自己解析该字符串。

关于c++ - 检查 Char/Int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32972801/

相关文章:

python - Pandas:迭代行并查找出现频率

java - java 将 if-else 转换为 switch 语句

c# - 优化 C# 中的 if 语句 (a > 0 && b > 0 && a + b == c)

MySQL IF 然后用数学

c++ - 如何检查一个范围内的值是否是另一个范围内的值的倍数?

c++ - MFC 到 QT 的转换

如果我不使用 Code::Blocks 但使用 cygwin 进行编译,c++ compare std::string 将不起作用

c++ - 如何在 QPlainTextEdit 中捕获链接点击事件

r - 在循环中汇总并生成多个变量

python - 视频播放速度慢 - Python 3.6、OpenCV 3、PyQT 5