我希望用戶輸入的日期等於或早於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;
}
你知道有星期幾的公式,你不必編寫一個列舉所有日期的億元if語句。 https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week – Barmar
類似於'if(day == 3 || day == 10 || day == 17 || day == 24 || day == 31)''可以簡化爲'if(day%7 == 3)'。 – Barmar
謝謝,我會從維基百科讀取它。 – Dem