+ 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…

7th Nov 2021, 1:56 PM
Felix L
Felix L - avatar
11 ответов
+ 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
7th Nov 2021, 5:11 PM
Lothar
Lothar - avatar
+ 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
7th Nov 2021, 6:30 PM
Lothar
Lothar - avatar
+ 2
Can you post the full code? You may just need to give all inputs at once
7th Nov 2021, 2:22 PM
Slick
Slick - avatar
+ 2
Try a while condition that every time your condition is true, which means false input, loop you back to your input
7th Nov 2021, 3:12 PM
Aggelos M
Aggelos M - avatar
+ 2
Yeah i thought about a while condition, but how can i put multiple conditions in one while loop?
7th Nov 2021, 4:49 PM
Felix L
Felix L - avatar
+ 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
8th Nov 2021, 2:44 AM
Jason Sykes
Jason Sykes - avatar
+ 1
Okay, i will try this… does || mean „and“?
7th Nov 2021, 5:05 PM
Felix L
Felix L - avatar
+ 1
AND operator is && and OR ||
7th Nov 2021, 5:30 PM
Aggelos M
Aggelos M - avatar
7th Nov 2021, 5:31 PM
Aggelos M
Aggelos M - avatar
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
9th Nov 2021, 1:54 PM
Kiwwi#
Kiwwi# - avatar
- 2
Okay, and just to understand it better, what is the logical AND operator?
7th Nov 2021, 5:19 PM
Felix L
Felix L - avatar