Simple While Loop...so I thought...

Ok so this has stumped me for literally around 2 hours...

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
27
28
29
30
31
32
33
34
35
36
    while(mpick != 4)
    {
        type_fast("Main Menu\n\n");
        type_fast("1. New Game\n");
        type_fast("2. Load Game\n");
        type_fast("3. Options\n");
        type_fast("4. Exit Game\n");
        cin >> mpick;
            switch(mpick)
            {
                case 1:
                {
                    break;
                }
                case 2:
                {
                    break;
                }
                case 3:
                {
                    break;
                }
                case 4:
                {
                    type_fast("Exiting Program...Press ENTER to Exit...\n\n");
                    cin.ignore();
                    cin.get();
                    break;
                }
                default:
                {
                    type_fast("\nYour choice is invalid. Press ENTER to continue...\n\n");
                    cin.ignore();
                    cin.get();
                }
            }


This loop seems simple enough....everything works as intended. One thing I DO NOT understand is this...if for "mpick" the user inputs a char/string, basically anything other than an integer, the loop becomes recursive...just loops forever and ignores cin >> mpick;

Can anyone explain why it would do that and lead me in the right direction to error checking for that...also, I would really prefer "hints" per se...vs the actual code to finish this...I gotta learn some how and just giving the answer in code makes me lazy...I don't want to be lazy. :)

Edit: for all intensive purposes, mpick was not assigned anything prior to running...
Last edited on
mpick is an integer, correct? In that case, trying to read a character into it will cause cin to enter an error state where it will no longer read until you clear it. A simple fix would be to simply read a character (which can hold anything, including a number) and switch on that.

Note: the phrase is "for all intents and purposes", not "for all intensive purposes"
Seriously, sometimes I feel so inadequate at times lol...yes and you're right about the phrase...now I feel a bit more ridiculous lol.

btw the fix worked thank you...appears I need to open up my thinking a bit...*sigh*
Topic archived. No new replies allowed.