【问题标题】:Program Infinitely Looping程序无限循环
【发布时间】:2023-03-29 21:03:02
【问题描述】:

这是我程序中代码的 sn-p。当我输入整数时,函数 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;
}

【问题讨论】:

    标签: c++ function loops infinite-loop


    【解决方案1】:

    我玩 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;
        }
    }
    }
    

    【讨论】:

      【解决方案2】:

      您可以尝试像这样刷新cin

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

      或按照@user4581301 的建议

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

      【讨论】:

      • 如果你对忽略设置一些界限会更好。推荐ignore(numeric_limits&lt;streamsize&gt;::max(), '\n')。直到 eol 或流结束的所有内容。这是极不可能的,但 INT_MAX 可能还不够。
      • @user4581301 - 我将您的建议添加到答案中
      猜你喜欢
      • 1970-01-01
      • 2014-12-16
      • 2018-03-21
      • 2015-02-07
      • 1970-01-01
      • 2015-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多