【发布时间】:2021-01-15 04:24:45
【问题描述】:
我想在下面的代码中阻止所有字母输入,你能帮我吗?
#include <iostream>
using namespace std;
int main()
{
cout<<"To close this program you need to type in -1 for the first input"<<endl;
int m, n;
do{
int counter1 = 0;
int counter2 = 0;
cout<<"Now you need to input two seperate natural numbers, and after that it calculates the difference of both numbers factors!"<<endl;
cout<<"First Input"<<endl;
cin>>m;
if(m==-1){
break;
}
cout<<"Second Input"<<endl;
cin>>n;
if(m<0 or n<0){
cout<<"ERROR - Only natural numbers are allowed!"<<endl;
}
else{
...
程序的其余部分只是数学运算。
【问题讨论】:
-
请发帖minimal reproducible example。
m和n声明在哪里? -
m 和 n 是整数
-
不能屏蔽输入但可以检测到无效输入。
-
只要
m和n的类型为int,尝试输入字母将失败,并将值0分配给变量。 -
对于您未向我们展示的代码,我们无法为您提供帮助。请包括minimal reproducible example。细节很重要