+ 1

Kindly guide me how can I check that entered variable "y" is not a number. I want to show an error msg if other than no entered

https://code.sololearn.com/co7s8wxeC0oI/?ref=app

9th Nov 2020, 2:24 PM
Muhammad Habib Tahir
Muhammad Habib Tahir - avatar
6 Réponses
+ 1
(I think) if you enter anything other than a number for int variable "y" it will contain a "0" (zero)...so with that in mind...try.. int y; cout << "Please enter a number!" << endl; cin >> y; if(y == 0){ cout << "Numbers on please"; exit(EXIT_FAILURE); } ................the rest of your code. (although it does work, I've only checked it against a handful alphabetical characters and symbols)
9th Nov 2020, 6:49 PM
rodwynnejones
rodwynnejones - avatar
0
Thanks a lot both of you sir, I will try it
11th Nov 2020, 2:38 PM
Muhammad Habib Tahir
Muhammad Habib Tahir - avatar
0
Sir Martin Taylor , your code is for validation of variable y, in between 2 to 10. Thanks for it. It's working for validation only.
12th Nov 2020, 12:37 PM
Muhammad Habib Tahir
Muhammad Habib Tahir - avatar
0
Sir rodwynnejones , I tried your suggestion, i.e if(y==0) , if(y >1) but not working. I found out that char s = -858993460
12th Nov 2020, 12:42 PM
Muhammad Habib Tahir
Muhammad Habib Tahir - avatar
0
Hi..sorry for the late reply..... I've just copied and pasted the code from my original post into my IDE and I've run it in codeplayground and it works as I thought, not sure why your getting -858993460 when you input "s". https://code.sololearn.com/cg2mFvo4U6Q6/#cpp
15th Nov 2020, 9:56 PM
rodwynnejones
rodwynnejones - avatar