當用戶輸入「y」或「Y」作爲「你是否希望進入另一年?(Y/N):」,循環「該月有0天」永遠。爲什麼? 我試着看看這些值,看起來好像存儲的值再次被使用。也許我沒有正確使用cin.clear()?forever loop ...沒有正確清除用戶輸入?
//variables
bool ucontinue = true; //answer to continue
int year = 0;
int month = 0;
int days = 0;
char answer = 'a';
//loop
while (ucontinue == true)
{
/*
Enter a year (Must be a positive integer): 2016
Enter a month (Must be a between 1 and 12): 2
The month has 29 days.
Do you wish to enter another year? (Y/N): y
*/
//year input
while (year <= 0)
{
cout << "Enter a year (Must be a positive integer): ";
cin >> year;
}
//month input
while (month <= 0)
{
cout << "Enter a month (Must be a between 1 and 12):";
cin >> month;
}
//# of days in the month
cout << "The month has " << days << " days." << endl << endl;
//continue?
while (answer != toupper('y') && answer != toupper('n'))
{
cout << "Do you wish to enter another year? (Y/N): ";
cin >> answer;
answer = toupper(answer);
if (answer == toupper('n'))
{
ucontinue = false;
}
}
cin.clear();
}
您的代碼不會更改'days'值。你需要根據'month'和'year'的輸入來改變它。您可能還需要一個功能來檢查「閏年」。 – Shadi
是的,我有psuedocode的日子,但我只是想確保其他while循環工作正常。謝謝! – Kimmie
該課程還沒有結束,所以我避免把它放在我的代碼中。 我以爲cin.ignore()會清除所有的用戶輸入 - 有沒有一個原因,爲什麼它不? – Kimmie