simplifying d0-while loop

i am making this program with nested functions,, i want to simplify the condition of "while". can you suggest any?

productlist()
{
char plchoice;

do{
cout<<"GRAPHICS AID ENGRAVING PRODUCTS\n";
list();
cout<<"\nO-Make an Order M-Main Menu E-Exit";
getline(cin, plchoice);
if (plchoice==O || plchoice o)
order();
else if (plchoice=='M' || plchoice=='m')
menu();
else if (plchoice=='E' || plchoice=='e')
escape();
else
{
cout<<"Error Input! Refer to the Choices!";
system ("cls");
}
}while (plchoice!='M' && plchoice!='m' && plchoice!='O' && plchoice!='o' && plchoice!='E' && plchoice!='e');
}
Why not make a function that checks for a valid condition?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
bool ValidChoice(char plchoice)
{
   if (plchoice != 'M' &&
       plchoice != 'm' &&
       plchoice != 'O' &&
       plchoice != 'o' &&
       plchoice != 'E' &&
       plchoice != 'e')
   {
       return true;
   }
   else
   {
       return false;
   }
}

// Then in your loop

do {

// Do stuff

} while (ValidChoice(plchoice))
what is the use of return true and return false?
Well, it's a boolean function, so it can return one of two values, true or false (actually, it's generally considered that 0 = false and anything not 0 = true).

So what your function would be doing here is checking to see if the choice passed into it matches one of those letters and, if it does, it returns a result of true. Your call to the function, in the while condition, is basically saying "do this code while ValidChoice returns true", meaning the code will execute every time true is returned from that function. If the function returns false (i.e. the 'else' part of the if statement in the function is hit), the loop will cease.
Topic archived. No new replies allowed.