For this program, create a (very simplified) day of the week calculator. Ask the
ID: 3888818 • Letter: F
Question
For this program,
create a (very simplified) day of the week calculator. Ask
the user to enter the day of the month, and respond with the correct day ofthe week.
The program will assume that
the month starts on Friday
and has 30 days
(just like the month of
September in
2017). The program
o
Can
assume that the numb
er entered will be an integer
o
C
annot
assume that the number entered will be valid!
If the day of the month the user entered is not a valid day of the month (less
than 1 or greater
than
30
), simply print a short error message to the user.
Otherwise
,
the day of the week that day falls on. For instance, the
2nd
would be a
Saturday
, the 10th would be a
Sunday
, etc.
IMPORTANT:
Do
not
write a case for each day of the month. If your program
uses
doze
n
s of
individual
if
,
elif
, or
else
statement
s, you will
lose
significant points
Explanation / Answer
/* Program to find the day of the given date - DAY.C */
#include <iostream>
#include <string.h>
#include <limits>
using namespace std;
int input (); //Function For input
int main()
{
int tdate, s=0; // variables for input date
char week[7][10] ;
strcpy(week[3], "Sunday") ;
strcpy(week[4], "Monday") ;
strcpy(week[5], "Tuesday") ;
strcpy(week[6], "Wednesday") ;
strcpy(week[7], "Thursday") ;
strcpy(week[1], "Friday") ;
strcpy(week[2], "Saturday") ;
tdate = input(); // calling functon for date
if (tdate> 30)// checking against valid input
{
cout <<" Ivalid input Date in September cant be more than 30 EXITING THE Program " ;
return 0;
}
else if (tdate<0) // checking against valid input
{
cout <<" Ivalid input Date can't be negative EXITING THE Program " ;
return 0;
}
tdate= tdate % 7; //taking modulus to reduce checking of all cases as all days are multiple of 7
switch(tdate) // to check against the number of days using switch
{
case 1 : s=1; // prints "Friday"
break; // and exits the switch
case 2 : s=2; // prints Satuarday
break;
case 3 : s=3; //prints sunday
break;
case 4 : s=4;// prints monday
break;
case 5 : s=5;
break;
case 6 : s=6;
break;
case 0 : s=7;
break;
}
cout<<" Weekday on this date is: " <<week[s]<<endl ;
return 0;
}
int input () // defination of input
{
int c;
cout<<" Enter the date of the month September. " ;
cin>>c;
if(!cin.good()) // checking against valide integar input
{
cout<<"ERROR, THAT WAS NOT NUMBERIC"<<endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), ' ' );
}
return c;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.