0

I am using c++. I created a simple "how old are u" programm and i can not find how it can recognise if the input a number.

so int a cout <<"how old are you \n "; cin >> a; if (a>10){ cout << "ok \n";} if (a ..............){ //if a is not a number cout <<"this is not a number \n";}

12th Oct 2018, 2:36 PM
Tasos T
Tasos T - avatar
9 Respuestas
+ 5
You have to connect multiple if statements with a logical operator like && (logical and) or || (logical or). if(age > 0 && age < 13) { cout << ... } Also, return main() doesn't make the program start over again. return always exists a function. Maybe it works if you just use main() (without return), but it is still an unusual thing to do 🤔
12th Oct 2018, 4:11 PM
Anna
Anna - avatar
+ 3
Check cin.fail() method, if it returns true (1) it means your input was not expected. int a; cout << "How old are you? "; cin >> a; // Check for validity here ... // fail() method returns true if // input was not of expected type if(cin.fail()) { cout << "\nThat was not a number \n"; } // More code follows ... Hth, cmiiw
12th Oct 2018, 3:28 PM
Ipang
+ 2
If the input is not a number, it can't be assigned to a (which is declared as an integer). a will probably be 0 in that case and you can check if a == 0.
12th Oct 2018, 2:55 PM
Anna
Anna - avatar
+ 2
Well, you should've put the code to check for cin.fail() immediately after receiving user input, right under "cin >> a;". So I decided to rewrite your code with some comments within, to explain some stuffs. If you need clarification about something you can comment in the code, I'll respond later, right now it's time for me to sleep : ) https://code.sololearn.com/cb3z02bir86e/?ref=app
12th Oct 2018, 7:32 PM
Ipang
+ 1
Thank u both for your asnwers. But i tried the first one and it did not work, and the second works only if i have return 0. But i want to start from the beginning. So here is my code total,if you are intrested to see it and understand better my problem #include <iostream> using namespace std; int main() { int a; cout <<"How old are you?\n"; //first question,the beginning cin >>a; if (a>=1) if(a<13){cout << "You are too young to worry. You are just a kid \n\n"; } if ( a>=13 )if (a<17){ cout << "You are a big child now \n\n"; } if ( a>=17)if (a<35){ cout << "You have a whole life in front of you \n\n"; } if (a>=35)if (a<50){ cout << "Do not worry, you are still young \n\n"; } if (a>=50)if (a<70){ cout << "I hope that you achieved everything you wanted to!!! \n\n"; } if (a>=70)if (a<100){ cout << "I hope that you will live more than 100 years old \n\n"; } if (a>=100){ cout << "You are the oldest person i have ever met \n\n"; } if (cin.fail()){ cout << "\n This is not a number. Put a new one\n"; //here is what you told me return main(); } int b; int c; cout <<"Do you want to change your age?(1 for Yes and 2 for No)\n\n"; //give the possibily of choosing again cin >> b ; switch( b ){ case 1: cout <<"Here you are!!!!\n\n"; return main(); break; case 2: cout <<"Ok bye!!!! \n"; return 0; default: cout << "I said 1 for Yes and 2 for No \n\n"; cin >>c; switch (c) //could not find other way to do it but it works { case 1: cout <<"Here you are!!!!\n\n"; return main(); break; case 2: cout <<"Ok bye!!!! \n\n"; return 0; break; default: cout<<"I am so sorry that you do not want to be serious. Bye \n ";
12th Oct 2018, 4:08 PM
Tasos T
Tasos T - avatar
+ 1
It did not change anything but still thank you. Also the second and third time (in the end) that i use return main() it works like i want to, so start from the beginning
12th Oct 2018, 4:17 PM
Tasos T
Tasos T - avatar
+ 1
Thank you for spending so much time making my code. I appreciate it. But codeblocks says that they are 3 errors (i did exactly what you have written), -line 56 error: 'numeric_limits' is not a member of 'std' -line 56 error: expected priamry-expressions before '>' token -line 56 error '::max' has not been declared I do not think i can solve these errors by my own because i am not so familiar with all these kind of stuff. If u have time to check it, i wοuld be grateful!
12th Oct 2018, 8:29 PM
Tasos T
Tasos T - avatar
+ 1
Tasos T, unfortunately I cannot check it out for you, as I don't have a PC/Laptop with me now, I code on mobile. However, a recent web search I did lead to Code::Blocks compiler configuration, if the Code::Blocks was configured to enable C++11 features there should *normally* be no problem running that code. http://www.cplusplus.com/doc/tutorial/introduction/codeblocks/ Another probability I also found (more relevant with Visual Studio) was that under Visual Studio there is a problem due to name conflicts of max() function included with windows.h header. The inclusion of max() function by windows.h conflicting the max() function from the numeric_limits. I can't assist you further due to the fact I'm not familiar with Code::Blocks. I hope some of our friends here who uses Code::Blocks can drop some comments/suggestions; if not, create new thread and bring the issue up to the community. Please save your code in profile and attach the URL in your original post (Description section) so everyone can see it.
13th Oct 2018, 3:06 AM
Ipang
+ 1
Ipang thank you again for your asnwers. I changed the compliers setting but still the same results. And i saved my initial code with the url as you told me
13th Oct 2018, 12:44 PM
Tasos T
Tasos T - avatar