Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a while loop but with more conditions C++

Tags:

c++

while-loop

cout << "Options:\n1: Change Name\n2: Change Password\n3: Change Address\n4: Withraw\n5: Deposit\nL: Log out\n>";
while (user_input2 != '1' && user_input2 != '2' && user_input2 != '3' && user_input2 != '4' && user_input2 != '5' && user_input2 != 'L')
{
    cout << "Invalid input";
}

So how do I just shortened the while conditions?

I tried doing:

cout << "Options:\n1: Change Name\n2: Change Password\n3: Change Address\n4: Withraw\n5: Deposit\nL: Log out\n>";
while (user_input2 != '1','2','3','4','5','L')
{
    cout << "Invalid input";
}

but it doesn't work.

edit1: "I added more hints to what I wanted to do"

like image 761
MJ Delos Santos Avatar asked Dec 07 '22 09:12

MJ Delos Santos


1 Answers

You can use the < and > operators for the range of '1' to '5', but you'll have to handle the check for 'L' explicitly:

while (user_input2 != 'L' && (user_input2 < '1' || user_input2 > '5'))
{
    cout << "Invalid input";
}
like image 161
Mureinik Avatar answered Dec 28 '22 05:12

Mureinik