2016-10-08 119 views
-1

這是我的程序代碼片段。當我輸入整數時,函數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; 
} 

回答

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; 
    } 
} 
} 
0

您可以嘗試刷新cin這樣的:

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

,或者如果你把那些無視一些範圍,由@ user4581301

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

更好的建議。 '建議忽略(numeric_limits :: max(),'\ n')'。所有的東西都要等到eol或者流結束。這是不太可能的,但是INT_MAX可能還不夠。 – user4581301

+0

@ user4581301 - 我將您的建議添加到了答案中 – 4386427