0

The following program will work when we enter int number.But when we enter character, how can show error message? I want to show error message when we enter characters in this program. What will need to do?

#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;
}
Mat
  • 202,337
  • 40
  • 393
  • 406
Arkar
  • 1

1 Answers1

0

You can check if something is a number using isdigit. The include file for isdigit is cctype. You can display and error message using std::cout<<"error message"; Example: if(!isdigit(num1)){std::cout<<"error message";} should work. And the "!" means not in c++.

Community
  • 1
  • 1
geizio
  • 105
  • 1
  • 16