2016-05-14 117 views
-2

我希望用戶輸入的日期等於或早於2016年5月30日,並且不低於2016年1月1日。如果輸入無效,則應該返回並向用戶再次提問。問題是do...while循環沒有循環,輸入無效後停止。下面的代碼:while while while not working properly

#include<iostream> 
using namespace std; 

int main(){ 
    int month,day,year; 
    char symbol; 

    do{ 
     cout<<"Enter date: "; 
     cin>>month>>symbol>>day>>symbol>>year; 

     if(symbol=='/'&&month<=05&&day<=31&&year==2016){ 
      cout<<month<<symbol<<day<<symbol<<year; 

      if(month==01){   // this code is for month of January 
       if(day==3||day==10||day==17||day==24||day==31){ 
        cout<<" is Sunday."; 
       } 
       else if(day==4||day==11||day==18||day==25){ 
        cout<<" is Monday."; 
       } 
       else if(day==5||day==12||day==19||day==26){ 
        cout<<" is Tuesday."; 
       } 
       else if(day==6||day==13||day==20||day==27){ 
        cout<<" is Wednesday."; 
       } 
       else if(day==7||day==14||day==21||day==28){ 
        cout<<" is Thursday."; 
       } 
       else if(day==1||day==8||day==15||day==22||day==29){ 
        cout<<" is Friday."; 
       } 
       else if(day==2||day==9||day==16||day==23||day==30){ 
        cout<<" is Saturday."; 
       } 

      } 
      else if(month==02){  //this code is for month of February 
       if(day==7||day==14||day==21||day==28){ 
        cout<<" is Sunday."; 
       } 
       else if(day==1||day==8||day==15||day==22||day==29){ 
        cout<<" is Monday."; 
       } 
       else if(day==2||day==9||day==16||day==23){ 
        cout<<" is Tuesday."; 
       } 
       else if(day==3||day==10||day==17||day==24){ 
        cout<<" is Wednesday."; 
       } 
       else if(day==4||day==11||day==18||day==25){ 
        cout<<" is Thursday."; 
       } 
       else if(day==5||day==12||day==19||day==26){ 
        cout<<" is Friday."; 
       } 
       else if(day==6||day==13||day==20||day==27){ 
        cout<<" is Saturday."; 
       } 
      } 
      else if(month==03){   //this code is for month of March 

       if(day==6||day==13||day==20||day==27){ 
        cout<<" is Sunday."; 
       } 
       else if(day==7||day==14||day==21||day==28){ 
        cout<<" is Monday."; 
       } 
       else if(day==1||day==18||day==15||day==22||day==29){ 
        cout<<" is Tuesday."; 
       } 
       else if(day==2||day==9||day==16||day==23||day==30){ 
        cout<<" is Wednesday."; 
       } 
       else if(day==3||day==10||day==17||day==24||day==31){ 
        cout<<" is Thursday."; 
       } 
       else if(day==4||day==11||day==18||day==25){ 
        cout<<" is Friday."; 
       } 
       else if(day==5||day==12||day==19||day==26){ 
        cout<<" is Saturday."; 
       } 
      } 
      else if(month==04){  //this code is for month of April 

       if(day==3||day==10||day==17||day==24){ 
        cout<<" is Sunday."; 
       } 
       else if(day==4||day==11||day==18||day==25){ 
        cout<<" is Monday."; 
       } 
       else if(day==5||day==12||day==19||day==26){ 
        cout<<" is Tuesday."; 
       } 
       else if(day==6||day==13||day==20||day==27){ 
        cout<<" is Wednesday."; 
       } 
       else if(day==7||day==14||day==21||day==28){ 
        cout<<" is Thursday."; 
       } 
       else if(day==1||day==8||day==15||day==22||day==29){ 
        cout<<" is Friday."; 
       } 
       else if(day==2||day==9||day==16||day==23||day==30){ 
        cout<<" is Saturday."; 
       } 
      } 
      else if(month==05){  //this code is for month of May 

       if(day==1||day==8||day==15|day==22||day==29){ 
        cout<<" is Sunday."; 
       } 
       else if(day==2||day==9||day==16||day==23||day==30){ 
        cout<<" is Monday."; 
       } 
       else if(day==3||day==10||day==17||day==24||day==31){ 
        cout<<" is Tuesday."; 
       } 
       else if(day==4||day==11||day==18||day==25){ 
        cout<<" is Wednesday."; 
       } 
       else if(day==5||day==12||day==19||day==26){ 
        cout<<" is Thursday."; 
       } 
       else if(day==6||day==13||day==20||day==27){ 
        cout<<" is Friday."; 
       } 
       else if(day==7||day==14||day==21||day==28){ 
        cout<<" is Saturday."; 
       } 
      } 
      break; 
     } 
     else{ 
      cout<<"You have entered an invalid input.\n"<<endl; 
     } 
    }while(symbol!='/'&&!(month<=05)&&!(day<=31)&&year!=2016); 
    return 0; 
} 
+0

你知道有星期幾的公式,你不必編寫一個列舉所有日期的億元if語句。 https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week – Barmar

+0

類似於'if(day == 3 || day == 10 || day == 17 || day == 24 || day == 31)''可以簡化爲'if(day%7 == 3)'。 – Barmar

+0

謝謝,我會從維基百科讀取它。 – Dem

回答

2
while(symbol!='/'&&!(month<=05)&&!(day<=31)&&year!=2016); 

必須

while(symbol!='/' || !(month<=05) || !(day<=31) || year!=2016); 

或@Barmar

的建議
while(symbol!='/' || (month>05) || (day>31) || year!=2016); 

你的原始表達式要求所有4是真實的,由於&&。使用||的表達式只需要其中一個是正確的。

另請注意,您不需要檢查,因爲您已經有if裏面。

所以,你可能只是這樣做:

while (true) { 
    cout<<"Enter date: "; 

    cin>>month>>symbol>>day>>symbol>>year; 

    // clear cin 
    cin.clear(); 
    cin.ignore(10000,'\n'); // Or better: 
          // std::numeric_limits<std::streamsize>::max() 
          // instead of just 10000 

    if(symbol=='/' && month<=05 && day<=31 && year==2016){ 
     // code for handling valid date here 

     // This will end the while loop 
     break; 
    } 
    else{ 
     cout<<"You have entered an invalid input.\n"<<endl; 
    } 
}; 

無關:還要注意此行

cin>>month>>symbol>>day>>symbol>>year; 
      ^^   ^^ 

在這裏,你讀入symbol兩次,即第二個值只是簡單地覆蓋第一。相反,您需要

cin >> month >> symbol1 >> day >> symbol2 >> year; 
        ^    ^

並導致相應代碼的其餘部分發生改變。

更新

的代碼可以在這裏https://ideone.com/KvCy1e

測試隨着輸入

dem/dem/dem 
1/1/2016 

輸出是

Enter date: You have entered an invalid input. 

Enter date: 1/1/2016 is Friday. 

更新2

正如評論注意到了@Barmar,你應該初始化變量的循環之前的一些無效的日期。

int month = 13; 
int day = 32; 
int year = 0; 
char symbol = 'x'; 

而btw:由於您使用ints您應該檢查負值。

+0

'!(月<= 5)'可以簡化爲'month> 5'。或者你可以使用德摩根定律結合所有的條件,這可能會使它更清楚。 – Barmar

+0

循環正在工作,但它不會停止輸出「輸入日期:您輸入了無效的輸入。」 ......和I'ts看起來像一個矩陣。呵呵。爲什麼是這樣? – Dem

+0

@Dem - 這必須是因爲輸入的值無效。真實的,它可以以不同的寫 - - 我想保持OP的風格,使 – 4386427