Do - While loop

My do-while loop isnt reading the entire bracketed area after 1 run (meaning the first run it actually reads the cin line). It only reads my cout line and skips the other lines. Why is this?

Im pretty new so if my technique is a little unorthodox please forgive me. I simply am trying to answer this question. After fixing it then make suggestions on proper code building would be helpful also unless proper code building is necessary to fix my problem :)


int main()
{
    int menu1 = 0;
    while(menu1 != 'q')
    {
        cout << "\t Help\t Smallest \t Largest \t Quit" << endl;
        cin >> menu1;
        GetMenu(menu1);
    }
    return 0;
}
Last edited on
What is happening in GetMenu()?

Its a simple switch / case but it doesnt do anything in it either. I ran the line by line debug and it actually goes to get menu but it doesnt abide by all the rules i have set.

void GetMenu(int x)
{
        switch(x)
        {
            case 'h':
            case 'H':
                help();
                break;
            case 's':
            case 'S':
                smallest();
                break;
            case 'l':
            case 'L':
                largest();
                break;
            case 'q':
            case 'Q':
                cout << "Exiting please wait...";
        }
        return;
}


I had everything together in main and it was doing the same thing... onlyr reading couts, ignoring my switch, cin and others... if i add another cout... it reads it but if i add 2 or 3 cin's it doesnt read it....infinite loop of ONLY cout...
Last edited on
use char as a data type instead of int for menu1
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
void GetMenu(char x)
{
        switch(x)
        {
            case 'h':
            case 'H':
                help();
                break;
            case 's':
            case 'S':
                smallest();
                break;
            case 'l':
            case 'L':
                largest();
                break;
            case 'q':
            case 'Q':
                cout << "Exiting please wait...";
        }
        return;
}

int main()
{
    char menu1 = '0';
    while(menu1 != 'q')
    {
        cout << "\t Help\t Smallest \t Largest \t Quit" << endl;
        cin >> menu1;
        GetMenu(menu1);
    }
    return 0;
}

tell me if it is working now ?
Last edited on
Doh :( thanks
You're doing a cin to an int. I'm surprised you even got to GetMenu. cin is going to choke on alpha input to an int. Change your declaration of menu1 to a char.
 
  char menu1 = 0;


Please use code tags (the <> formatting button) rather than quote when posting code.
[code]
code here
[/code]


Yeah that was it. Noob mistake, i should have caught that.
worked?
Yesir!
AbstractionAnon I will in the future, Thanks.
Topic archived. No new replies allowed.