0
Is it possible to make it say error when you write text instead of numbers?
If you know how to could you tell me because I'm new to c++ so I need a little help https://code.sololearn.com/cXM5ULYv3277/?ref=app
3 Respostas
0
Yes. Try this way...
int a;
cin >> a; //if incompatible input, cin fails.. So
if(cin.fail())
{
cout<<"error, invlid input";
exit(1);
}
0
I mean to add it just after cin as I mentioned. Not at last
Edit : Lugli
#include <iostream>
using namespace std;
int main()
{
int a;
int b = 6;
cin >> a;
if(cin.fail())
{
cout<<"error, invalid input";
exit(1);
}
if( a>b )
cout << "its greater than 6";
else if(a<b)
cout << "its smaller than 6";
else
cout<<"both equal";
return 0;
}
0
Jayakrishna🇮🇳 Thank you man