do-while loops with multiple conditions

im having an issue with do-while loops, i got input just fine from this site, but im having an issue getting it to accept more than one value as acceptable to pass through the loop.

char MarStat;
do{

std::cout << "Are you Single(S) or Married(M)?\n";
std::cin >> MarStat;
toupper(MarStat);
cout << "Checking...\n";

}while (MarStat == 'M');

works fine but

do{

std::cout << "Are you Single(S) or Married(M)?\n";
std::cin >> MarStat;
toupper(MarStat);
cout << "Checking...\n";

}while (MarStat == 'M' || MarStat == 'S');

doesnt?
and how would i get it to recognize both those so if it gets either, like if it was a switch statment, but confirming the values right after they were entered to see if either one pops up, then continues, not stays in the loop.

Thanks
this seemed to work. trial and error.
do {


std::cout << "Are you Single(S) or Married(M)?\n";
std::cin >> MarStat;
MarStat = toupper(MarStat);


}while (!(MarStat == 'M' || 'S'));
Both of those are wrong.

Remember that the loop will keep going as long as the condition is TRUE.. Therefore:

}while (MarStat == 'M' || MarStat == 'S');

Will keep looping as long as the input is either M or S (ie: will keep looping as long as the input is valid -- the exact opposite of what you want).

Also...
}while (!(MarStat == 'M' || 'S'));

Since 'S' always evaluates to true, the ! makes this expression always evaluate to false, resulting in you NEVER looping, regardless of what the user inputs.

What you want is to loop while the input is invalid... so you'd want something like the following:

}while( (MarStat != 'M') && (MarStat != 'S') );
I see, will that only break out of the loop if both statements are true though?
yes, but notice the conditions are !=.

So it will loop as long as MarStat is not 'M' and not 'S'.

Which means it will loop until MarStat is 'M' or is 'S'.
I see now. That makes sense. Thank you much disch.
I personally find constructs like the following to be more useful to the end user:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
char MaritalStatus;

// Ask the user the question
cout << "Are you single or married? " << flush;

while (true)
  {
  // Get the user's answer.
  string s;
  cin >> skipws;
  if (!getline( cin, s )) break;

  // Validate it
  if (!s.empty())
    {
    MaritalStatus = toupper( s[ 0 ] );
    if (string( "MS" ).find( MaritalStatus ) != string::npos)
      break;
    }

  // Complain to those who need more instruction
  cout << "Enter \"single\" or \"married\": " << flush;
  }

// If we get this far, either EOF is encountered or the user input properly
cout << "Good job! Your marital status is '" << MaritalStatus << "'.\n";

So:
Are you single or married? yes
Enter "single" or "married": no
Enter "single" or "married":
Enter "single" or "married": -7
Enter "single" or "married": m
Good job! Your marital status is 'M'.

BTW, http://en.wikipedia.org/wiki/Marital_status

Hope this helps.
Topic archived. No new replies allowed.