Pergunta

Currently I have a working program, but am trying different things to see if they work. below is the code I am trying to get to work but having a function that asks the user to make a selection. I want to return that value into my main function which will run a do while loop that includes a case. Originally I have it as an if else if statement, but want to cut that down if possible. Below is the code I am working with. When running the program it only returns the entire menu instead of running the function from the value that should be returned.

 {
        int choice;
        createEmptySeats();
        seatPrice();

        do {
            choice = menu();
            switch (choice)
            {
            case '1': 
                reserveSeat();
                break;
            case '2': 
                displayInfo();
                break;
            case '3': 
                DisplaySummary();
                break;
            case '4': 
                break;
            }
        }
        while (!seatFull());

        return 0;
    }


        int menu() 
        {
            int userSelection = -1;
            while (userSelection < 1 || userSelection > 4) 
            {
                cout << "Please Choose Option Below" << endl;
                cout << "1: Reserve Seat(s)"<<endl;
                cout << "2: Display Available Seats" << endl;
                cout << "3: Display Information:" << endl;
                cout << "4: Exit System" << endl;
                cin >> userSelection;
            }
            return userSelection;
        }

Foi útil?

Solução

First: You're missing break; in your switch statement for each of the cases, except case 4 (not sure if it's by design).

Second: Choice is int, your cases are chars. Change one of them, so that both of them match. You're never finding a match for your case, so you just keep on looping.

Outras dicas

choice is int, and you write char - '1', '2' in the case. Write as follows:

    choice = menu();
    switch (choice)
    {
    case 1: 
        reserveSeat();
        break;
    case 2: 
        displayInfo();
        break;
    // ...

In your code you can come to case '1' when choice is an ASCII code of '1', i.e. 49. In your code it could never happen, so you stuck in the infinite loop with menu().

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top