[try Beta version]
Not logged in

 
 
Do - While loop

Mar 6, 2013 at 6:54pm
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 Mar 6, 2013 at 7:03pm
Mar 6, 2013 at 7:03pm
What is happening in GetMenu()?

Mar 6, 2013 at 7:05pm
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 Mar 6, 2013 at 7:07pm
Mar 6, 2013 at 7:12pm
use char as a data type instead of int for menu1
Mar 6, 2013 at 7:13pm
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 Mar 6, 2013 at 7:14pm
Mar 6, 2013 at 7:14pm
Doh :( thanks
Mar 6, 2013 at 7:18pm
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]


Mar 6, 2013 at 7:19pm
Yeah that was it. Noob mistake, i should have caught that.
Mar 6, 2013 at 7:19pm
worked?
Mar 6, 2013 at 7:23pm
Yesir!
Mar 6, 2013 at 7:24pm
AbstractionAnon I will in the future, Thanks.
Topic archived. No new replies allowed.