程序无限循环

时间:2016-10-08 05:48:10

标签: c++ function loops infinite-loop

这是我程序代码的片段。当我输入整数时,函数getMenuChoice()在Visual Studio上成功运行,但是当我输入一个字符时,它进入无限循环。我被告知我的程序不必考虑用户输入字符但是当我提交它时,评分机告诉我它超过了允许的长度"。除了它不考虑字符的事实,我不确定该功能有什么问题。如果我要考虑角色,我该怎么做?

// Calls header and menu.
int main() {
    printHeading();
    getMenuChoice();
}

//Prints the header.
void printHeading() {
    cout << "*******************************" << endl
         << "      Birthday Calculator      " << endl
         << "*******************************" << endl << endl;
}

//Prints the closer.
void printCloser() {
    cout << endl;
    cout << "****************************************************" << endl
    << "      Thanks for using the Birthday Calculator      " << endl
    << "****************************************************" << endl
    << endl;
}
void printMenu() {
    cout << endl << endl;
    cout << "Menu Options" << endl
    << "------------" << endl;
    cout << "1) Determine day of birth" << endl;
    cout << "2) Print the next 10 leap years" << endl;
    cout << "3) Determine birthdays for the next 10 years" << endl;
    cout << "4) Finished" << endl << endl;
    cout << "Choice --> ";
}

//Gets user's menu choice.
int getMenuChoice() {
    int choice;
    printMenu();
    cin >> choice;

    //If user does not select 4, it selects their menu choice.
    while (choice != 4) {
        if (choice == 1) {
            determineDayOfBirth();
        }
        else if (choice == 2) {
            print10LeapYears();
        }
        else if (choice == 3) {
            print10Birthdays();
        }

        //User did not enter a valid choice and the following prints.
        else {
            cout << "Invalid menu choice" << endl;
        }

        //Allows the user to enter another choice
        //after they have executed one choice.
        printMenu();
        cin >> choice;
    }

    //Prints closer when user chooses 4.
    printCloser();
    return 0;
}

2 个答案:

答案 0 :(得分:0)

自从我玩C ++以来已经有一段时间,但这是一个镜头。

int choice;
printMenu();
cin >> choice;
ValidateOption(choice);

// Then if you want them to be able to pick again you can just do it over again
printMenu();
cin >> choice;
ValidateOption(choice);

function ValidateOption(int choice){
//If user does not select 4, it selects their menu choice.
while (choice != 4) {
    if (choice == 1) {
        determineDayOfBirth();
    }

    else if (choice == 2) {
        print10LeapYears();
    }

    else if (choice == 3) {
        print10Birthdays();
    }

    //Prints closer when user chooses 4.
    else if (choice == 4){
        printCloser();
        return 0;
    }
    //User did not enter a valid choice and the following prints.
    else {
        cout << "Invalid menu choice" << endl;
        // Make the user select again because the input was invalid    
        printMenu();
        cin >> choice;
    }
}
}

答案 1 :(得分:0)

您可以尝试像这样刷新cin

cin >> choice;
cin.clear();
cin.ignore(INT_MAX);

或@ user4581301

的建议
cin.ignore(numeric_limits<streamsize>::max(), '\n');