I want the user to input a date that falls before or equal to May 30,2016 and not below than January 1,2016. If the input is not valid then it should go back and ask question again to the user. The problem is that do...while
loop is not looping, it stops after the input is invalid. Here's the code:
#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;
}
while(symbol!='/'&&!(month<=05)&&!(day<=31)&&year!=2016);
must be
while(symbol!='/' || !(month<=05) || !(day<=31) || year!=2016);
or as suggested by @Barmar
while(symbol!='/' || (month>05) || (day>31) || year!=2016);
Your original expression requires all 4 to be true due to the &&
. The expression using ||
requires just one of them to be true.
Also notice that you don't need that check as you already have an if
inside.
So you could just do:
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;
}
};
Unrelated: Also notice this line
cin>>month>>symbol>>day>>symbol>>year;
^^ ^^
Here you read into symbol
twice, ie the second value will just overwrite the first. Instead you would need
cin >> month >> symbol1 >> day >> symbol2 >> year;
^ ^
and of cause change the rest of the code accordingly.
Update
The code can be tested here https://ideone.com/KvCy1e
With the input
dem/dem/dem
1/1/2016
the output is
Enter date: You have entered an invalid input.
Enter date: 1/1/2016 is Friday.
Update 2
As noticed by @Barmar in a comment, you should initialize your variables to some invalid date before the loop.
int month = 13;
int day = 32;
int year = 0;
char symbol = 'x';
And btw: Since you use ints
you should check for negative values.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.