Lucho Mansilla
Lucho Mansilla

Reputation: 31

Cant make the loop work properly

The code below is supposed to let me choose an option from the menu and then display the appropriate line from the switch statement. It only works when I choose option 'g'. If I choose 'c', it displays the cout line but then it skips the loop and the program exits. What am I doing wrong?

#include <iostream>
#include <cctype>

using namespace std;
enum {c='c', p='p', t='t', g='g'}; // 0-3

int main(){
    cout
            << "Elija una opcion del menu:" << endl
            << "c) carnivoros" << endl
            << "p) pianista" << endl
            << "t) Arbol" << endl
            << "g) Juegos" << endl;
    char eleccion;
    cin.get(eleccion);

    while (eleccion >= c && eleccion <= g){
            switch (eleccion)
            {
            case 'c' : cout << "Te gusta la carne e!\n"; break;
            case 'p' : cout << "Sos pianista?\n"; break;
            case 't' : cout << "Naturista!\n"; break;
            case 'g' : cout << "Vicio!\n"; break;  
            }
            cout
                    << "Elija una opcion del menu:" << endl;
            cin.get(eleccion);             
    }
_getche();
return 0;
}

Upvotes: 0

Views: 93

Answers (2)

MikeMB
MikeMB

Reputation: 21136

You have two problems in your code:

  1. cin.get(eleccion); doesn't clear the new line character from the input stream, so right the next time cin.get is called, it will read the newline character, which is outside the range you specified and it will terminate the loop. Use cin >> eleccion; instead to fix that.

  2. As you assign ASCII letters to your enum, the bigges enum is NOT g but t, so your loop header should look like this:

    while (  c <= eleccion && eleccion <= t){ 
    

This version still has the drawback, that your loop continues if you enter any char between c and t (which si most of the alphabet) even if it is not a valid selection. For that reason I'd change the program as follows:

using namespace std;
//enums are not needed
int main(){
    cout << "Opciones:" << endl
        << "c) carnivoros" << endl
        << "p) pianista" << endl
        << "t) Arbol" << endl
        << "g) Juegos" << endl;


    bool rightChar = true;

    while (rightChar){
        cout << "Elija una opcion del menu:" << endl;

        char eleccion;
        cin >> eleccion;
        switch (eleccion)
        {
            case 'c': cout << "Te gusta la carne e!\n"; break;
            case 'p': cout << "Sos pianista?\n"; break;
            case 't': cout << "Naturista!\n"; break;
            case 'g': cout << "Vicio!\n"; break;
            default: rightChar = false;
        }
    }

    return 0;
}

Upvotes: 3

Lord Zsolt
Lord Zsolt

Reputation: 6557

(eleccion >= c && eleccion <= g)

This line breaks it.

enum {c='c', p='p', t='t', g='g'}; // 0-3

The enum won't be c=0 ... g=3, but it will be c = 99, p = 112, t = 116, g = 103,

Thus, your if will turn into:

(eleccion >= 99 && eleccion <= 103)

And 112 and 116 clearly don't pass that condition.


Also as LightningRacisinOrbit pointed it out, you'll have a problem with the newline remaining in the buffer as well. A solution to it would be to use cin >> eleccion as MikeMB suggested.


You could change the condition in the if statement to

(eleccion >= c && eleccion <= t)

However it's still very ugly. Probably this would be the best:

(eleccion == c || eleccion == g || eleccion == p || eleccion == t)

Upvotes: 1

Related Questions