1

大家好,有人可以帮我修复我的这段代码吗?它运行但它不执行它应该做的任务。一旦你运行它,它会自动跳转到默认语句并跳过案例。请帮我解决这个问题谢谢!

#include<iostream.h>
#include<conio.h>

using namespace std;


int main()
{
    int choice;
    char nbaPlayer, tele, food, subject, x;

    cout << "This program determines your favorites.\n\n";
    cout << "Please select the number of your corresponding choice.";
    cout << "\n1. NBA Player";
    cout << "\n2. Teleserye";
    cout << "\n3. Food";
    cout << "\n4. Subject";
    cout << "\n5. Exit";

    switch (choice)
    {
        case 1:
            cout << "You have chosen NBA Player.\n";
            cout << "Please enter your favorite NBA Player. \n";
            cin >> nbaPlayer;
            cout << "Your favorite NBA player is " << nbaPlayer;
            break;

        case 2:
            cout << "You have chosen Teleserye.\n";
            cout << "Please enter your favorite teleserye. \n";
            cin >> tele;
            cout << "Your favorite teleserye is " << tele;
            break;

        case 3:
            cout << "You have chosen food.\n";
            cout << "Please enter your favorite food. \n";
            cin >> food;
            cout << "Your favorite food is " << food;
            break;

        case 4:
            cout << "You have chosen subject.\n";
            cout << "Please enter your favorite subject. \n";
            cin >> subject;
            cout << "Your favorite subject is " << subject;
            break;

        case 5:
            cout << "You chose to exit.\n";
            break;

        default:
            cout <<"\nInvalid input";

    }

    getch();
}
4

1 回答 1

11

你只是忘记了:

cin>>choice

在您显示可用的选择选项之后。所以当你从不给选择赋值时,它总是会选择default.

于 2013-07-21T08:04:42.717 回答