当我们在下面的程序中输入字符时如何解决(c ++)

时间:2018-03-24 18:55:48

标签: c++

当我们输入int number时,以下程序将起作用。但是当我们输入字符时,如何显示错误信息? 我想在这个程序中输入字符时显示错误信息。 需要做什么?

#include <iostream>

using namespace std;

int main()
{
int num1,num2,num3;
char c;
A:
cout << "\nEnter the three numbers:" <<endl;
cout <<"Num1: ";
cin>>num1;
cout <<"Num2: ";
cin>>num2;
cout <<"Num3: ";
cin>>num3;
if(num1==c)goto A;
if (num1 >num2 && num1>num3)cout<<"The largest number is: " <<num1<<endl;
else if(num2 >num1 && num2>num3)cout<<"The largest number is: " <<num2<<endl;
else if(num3 >num1 && num3>num2)cout<<"The largest number is: " <<num3<<endl;
if(num1 <num2 && num1<num3)cout<<"The smallest number is: " <<num1<<endl;
else if(num2 <num1 && num2<num3)cout<<"The smallest number is: " 
<<num2<<endl;
else if(num3 <num1 && num3<num2)cout<<"The smallest number is: " 
<<num3<<endl;
if(num1==num2 && num1<num3)cout<<"The smallest numbers are: " <<num1<<" and " 
<<num2<<endl;
else if(num1==num3 && num1<num2)cout<<"The smallest numbers are: " <<num1<<" 
and "<<num3<<endl;
else if(num2==num3 && num2<num1)cout<<"The smallest numbers are: " <<num2<<" 
and "<<num3<<endl;
if(num1==num2 && num1>num3)cout<<"The largest numbers are: " <<num1<<" and " 
<<num2<<endl;
else if(num1==num3 && num1>num2)cout<<"The largest numbers are: " <<num1<<" 
and "<<num3<<endl;
else if(num2==num3 && num2>num1)cout<<"The largest numbers are: " <<num2<<" 
 and "<<num3<<endl;
if(num1==num2 && num2==num3)cout <<"You Can't enter same numbers\nPlease try 
again"<<endl;
}

1 个答案:

答案 0 :(得分:0)

您可以使用isdigit检查某些内容是否为数字。 isdigit的包含文件是cctype。您可以使用std::cout<<"error message";显示和显示错误消息示例:if(!isdigit(num1)){std::cout<<"error message";}应该有效。和“!”意思是不是用c ++。