do/while loop not exiting

I am looping a switch/case statement with a do while loop, the beginning part asks the user to enter two numbers (these correspond to rows/columns of my 2d array), if the user enters Q the loop should exit, instead I believe it's going to the default case which exits the program, even when not exiting the program and a break; is put in there it still infinitely loops, when commenting out the entire default section it still infinitely loops.

Any help is greatly appreciated, didn't want to post all my code as there's quite a bit, will try provide what ever though.

my do while is pretty much:
1
2
3
4
5
6
7
8
9
 do{
    case --
    case --
    case --
    case --
    default: 
            cout << "blah blah";
             return 0;
while (selection1 != Q);
Why don't you paste the original code snippet, cuz what you're giving has quite a few problems, e.g. (selectionl != 'Q');.
Last edited on
.
Last edited on
closed account (zb0S216C)
rej3kt wrote:
if the user enters Q the loop should exit (sic)

Is Q a variable or the character literal Q?

Judging by the code you posted, none of the cases have an associated break statement. Therefore, the following case statement is processed until break is found.

The do...while loop is designed to loop at least once, regardless. If none of the cases handle your input, default will handle it. Consider a while loop.

Wazzak
Last edited on
Sorry, I'm a little confused. Q is just a character, if entered when selecting the first number to put into the array it should exit the loop.

All the cases have a break statement apart from default at the moment don't they?
closed account (zb0S216C)
Every character literal is a number. In fact, characters are just numbers, but shown as characters. When working with integers, a character is implicitly converted to its numerical ASCII equivalent decimal value. This code exemplifies this:

1
2
if(int('A') == 100)
    std::cout << "Match" << std::endl;

The above proposition will evaluate to false because the decimal equivalent of 'A' is 65, not 100.

Wazzak
Last edited on
Okay, I understand that, about the ASCII equivalent of characters. I'm just confused what that has to do with the loop.

In regards to the do while loop are you trying to say something like: The loop is going to run all the way through no matter what I enter because it is started before selection1 is entered. What I need to do is enter a while loop somewhere after the question has been asked.

If so I'm really struggling to see where I would put the while loop.
closed account (zb0S216C)
Since a do...while loop is guaranteed to loop at least once before being tested, entering 'Q' will trigger the default handler; thus, returning from the function altogether. Instead of returning, maybe you want continue. continue forces the loop (in which it was used) to finish the current cycle by skipping any following statements and start a new cycle.

Wazzak
Last edited on
Sorry, really not getting this. I just can't wrap my head around where I'd add in the continue statement or what the problem is.

It seems like typing in Q would take me to the default case statement which exits the whole program. I've tried added continue instead of return 0; into the default statement and that just infinetly loops my program.

edit: Solved it with an if statement after the input, thanks for the help though
Last edited on
Topic archived. No new replies allowed.