After the user inputs the date, month, and year, nothing will output. Can someone help please?
#include <iostream>
#include <cmath>
using namespace std;
int date;
int month;
int year;
int getCenturyValue;
int getYearValue;
int getMonthValue;
int DayoftheWeek;
bool isLeapYear;
int main()
{
cout<<"Please enter the date in the form of: DD"<<endl;
cin>>date;
cout<<"Please enter the month as a number"<<endl;
cin>>month;
cout<<"Please enter the year in the form of YYYY"<<endl;
cin>>year;
That's why your program will not display any output after user inputs the date, month, and year
and why are you taking these values from user when you have calculated them in your program.
There are so many errors other than that in your program.
From better code writing point of view, you should use switch in place of so many nested if-else
and you can also try to handle wrong user inputs in such a program
#include <iostream>
//#include <cmath>
usingnamespace std;
int main()
{
int nDate=0, nMonth=0, nYear=0;
do
{
cout<<"Please enter the date in the form of: DD"<<endl;
cin>>nDate;
}
while((nDate < 1) || (nDate > 31));
do
{
cout<<"Please enter the month as a number"<<endl;
cin>>nMonth;
}
while((nMonth < 1) || (nMonth > 12));
cout<<"Please enter the year in the form of YYYY"<<endl;
cin>>nYear;
bool bIsLeapYear = !(nYear%400) || (!(nYear%4) && (nYear%100));
nYear--;
nYear %= 400;
int DayoftheWeek = 0;
switch(nYear/100)
{
case 1:
DayoftheWeek += 5;
break;
case 2:
DayoftheWeek += 3;
break;
case 3:
DayoftheWeek += 1;
break;
}
nYear %= 100;
DayoftheWeek += nYear + (nYear/4);
switch(nMonth)
{
case 2:
if(nDate >= 29+bIsLeapYear)
{
cout<<"February month has no <<DayoftheWeek<<th day"<<endl;
return 1;
}
DayoftheWeek +=3;
break;
case 3:
DayoftheWeek +=3;
break;
case 4:
if(nDate == 31)
{
cout<<"April month has no 31st day"<<endl;
return 1;
}
DayoftheWeek +=6;
break;
case 5:
DayoftheWeek +=1;
break;
case 6:
if(nDate == 31)
{
cout<<"June month has no 31st day"<<endl;
return 1;
}
DayoftheWeek +=4;
break;
case 7:
DayoftheWeek +=6;
break;
case 8:
DayoftheWeek +=2;
break;
case 9:
if(nDate == 31)
{
cout<<"September month has no 31st day"<<endl;
return 1;
}
DayoftheWeek +=5;
break;
case 10:
break;
case 11:
if(nDate == 31)
{
cout<<"November month has no 31st day"<<endl;
return 1;
}
DayoftheWeek +=3;
break;
case 12:
DayoftheWeek +=5;
break;
}
if(nMonth > 2)
DayoftheWeek += bIsLeapYear;
DayoftheWeek += nDate;
DayoftheWeek %= 7;
cout<<"The day of the week was a ";
switch(DayoftheWeek)
{
case 0:
cout<<"sunday";
break;
case 1:
cout<<"monday";
break;
case 2:
cout<<"tuesday";
break;
case 3:
cout<<"wednesday";
break;
case 4:
cout<<"thursday";
break;
case 5:
cout<<"friday";
break;
case 6:
cout<<"saturday";
break;
default:
cout<<"unknown";
break;
}
cout<<endl;
return 0;
}