mystycs mystycs - 1 year ago 107
C++ Question

While loop keeps repeating

I setup a while loop where i want to choose r or h, i dont want to use forloops but i want to use a switch why is it when i enter r or h it keeps repeating a million times the cout for that case? I cant get it to just say it once..

while (chooseMove == 'r' or 'h')
switch (chooseMove)
case 'r':
cout << "you chose r";

case 'h':
cout << "you chose h";


I also tried it with forloops and had the same problem i cant figure it out

Answer Source

What you mean is while (chooseMove == 'r' or chooseMove == 'h'). What you've currently written is equivalent to ((chooseMove == 'r') or ('h')), and 'h' evaluates as true.

Maybe you were also asking for help with the input logic:

char c;
bool success = false;

while (std::in >> c)
  switch(c) {
    case 'r': /* ... */ success = true; break;
    case 'h': /* ... */ success = true; break;

  if (success) break;

This will also terminate if the input stream is closed, and you can use success to inspect the success of the operation.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download