+ 2
C++ loop to verify user input
Hey guys, i am just learning C++ and in tried to check, if a user input meets multiple conditions. So i store two integers (user input) in two different variables, for example „weeks“ and „days“. now i want to take a new user input, if weeks<0 OR days<0 OR days>6, but i have no idea how this works… If i write: if (weeks<0) { cout<<„false“<<endl; cin>>weeks>>days; } It Prints the text, but i cant take a new input…
13 Réponses
+ 6
i like the do... while loop here. when checking 2 different variables, you should also use 2 of these loops.
'||' is the logical 'OR' operator
+ 6
Felix L ,
you can find things like this in the community section by using the search bar. here is a short tutorial about logical operators:
https://www.sololearn.com/learn/CPlusPlus/1619/?ref=app
+ 2
Can you post the full code? You may just need to give all inputs at once
+ 2
Try a while condition that every time your condition is true, which means false input, loop you back to your input
+ 2
Yeah i thought about a while condition, but how can i put multiple conditions in one while loop?
+ 2
You should also wrap the input int a try catch. This will allow you to control your exceptions. For example:
do{
try{
cin>>number;
if(!cin)
} catch (...){
cout << "User input error" << endl;
}
Phone is dieing. Good luck
+ 1
Okay, i will try this… does || mean „and“?
+ 1
AND operator is && and OR ||
+ 1
https://www.cplusplus.com/doc/tutorial/operators/ this may help you
0
basically to compliment the other comments, it will not be as simple as a for loop; typically you eould use an object or static class to handle the process.
There are functions and so in the standard library that could be useful, though
- 2
Okay, and just to understand it better, what is the logical AND operator?