小编use*_*546的帖子

检查cin输入流会产生一个整数

我输入了这个,它要求用户输入两个整数,然后变成变量.从那里它将执行简单的操作.

如何让计算机检查输入的内容是否为整数?如果没有,请让用户键入一个整数.例如:如果有人输入"a"而不是2,那么它会告诉他们重新输入一个数字.

谢谢

 #include <iostream>
using namespace std;

int main ()
{

    int firstvariable;
    int secondvariable;
    float float1;
    float float2;

    cout << "Please enter two integers and then press Enter:" << endl;
    cin >> firstvariable;
    cin >> secondvariable;

    cout << "Time for some simple mathematical operations:\n" << endl;

    cout << "The sum:\n " << firstvariable << "+" << secondvariable 
        <<"="<< firstvariable + secondvariable << "\n " << endl;

}
Run Code Online (Sandbox Code Playgroud)

c++ cin

13
推荐指数
1
解决办法
14万
查看次数

标签 统计

c++ ×1

cin ×1