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

29th Sep 2020, 8:40 PM
Lugli
Lugli - avatar
3 Antworten
0
Yes. Try this way... int a; cin >> a; //if incompatible input, cin fails.. So if(cin.fail()) { cout<<"error, invlid input"; exit(1); }
29th Sep 2020, 8:48 PM
Jayakrishna 🇮🇳
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; }
29th Sep 2020, 9:16 PM
Jayakrishna 🇮🇳
0
Jayakrishna🇮🇳 Thank you man
29th Sep 2020, 9:27 PM
Lugli
Lugli - avatar