2017-05-14 20 views
-2

當用戶輸入「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(); 

} 
+3

您的代碼不會更改'days'值。你需要根據'month'和'year'的輸入來改變它。您可能還需要一個功能來檢查「閏年」。 – Shadi

+0

是的,我有psuedocode的日子,但我只是想確保其他while循環工作正常。謝謝! – Kimmie

+0

該課程還沒有結束,所以我避免把它放在我的代碼中。 我以爲cin.ignore()會清除所有的用戶輸入 - 有沒有一個原因,爲什麼它不? – Kimmie

回答

0

如果我理解正確的話,你做到以下幾點:

Enter a year (Must be a positive integer): 2017 
Enter a month (Must be a between 1 and 12):5 
The month has 0 days. 

Do you wish to enter another year? (Y/N): y <ENTER> 

和你的程序循環。接下來會發生什麼:

cin.clear(); 

被執行並且您的循環再次啓動。此時仍然設置了yearmonth。因此,在新的循環反覆行

while (year <= 0) 

遇到當條件爲假,循環繼續

while (month <= 0) 

此行也是如此。此後

cout << "The month has " << days << " days." << endl << endl; 

被印刷和在

while (answer != toupper('y') && answer != toupper('n')) 

的條件進行檢查。當我剛進入這個y條件是不正確的,然後執行

cin.clear(); 

,這個循環重新開始循環往復之後。

+0

所以:將除ucontinue外的所有變量移到外部循環中。這解決了循環問題,只留下了計算時間。正如其他地方所建議的那樣:toupper上的常量沒有用處 - 請測試大寫常量。 – Zastai

+0

另外,月可以是0或大於12. –

+0

他或她需要做的是語句而不是while語句,這也將解決它。對? – Tyger

0

你的程序有很多問題

  1. 你永遠不更新,或者在天變量輸入的內容,以免將始終爲0,你在程序的開始設置

  2. if (answer == toupper('n'))可簡化爲if (answer=='N')

+0

儘管這兩個問題都沒有關係到循環的問題。 – Zastai

1

因爲你,你永遠代碼循環while while while while while while while循環第一次運行程序它工作正常,但第二次它去周圍所有的值設置比如你通過循環走這while語句的第二次

while (year <= 0) 
{ 
    cout << "Enter a year (Must be a positive integer): "; 
    cin >> year; 
} 

將無法​​運行,因爲今年已經是大於0且這種情況的代碼中的所有while語句。如果你有while語句而不是while語句,那麼會起作用的是因爲while語句在測試條件之前會通過循環運行一次。像這樣:

do 
{ 
    cout << "Do you wish to enter another year? (Y/N): "; 
    cin >> answer; 
    answer = toupper(answer); 


    if (answer == toupper('n')) 
    { 
     ucontinue = false; 
    } 

}while(answer != 'Y' && answer != 'N'); 
+0

謝謝。我只是決定將除了繼續之外的所有變量移到外部while循環中。我也試圖遠離實時循環,因爲班級還沒有完成。 我想知道爲什麼cin.ignore()不能清除用戶輸入? – Kimmie

+0

我想你誤解了cin.ignore()[鏈接](https://stackoverflow.com/questions/5131647/why-would-we-call-cin-clear-and-cin-ignore-after-reading-input) – Tyger

+0

如果通過明確的用戶輸入,你的意思是它應該清除「year,month,answer'內的值,這是不可能的,但你可以在第一個開頭設置'year = 0,month = 0,answer ='a''循環,這也應該解決問題 – Tyger