Question
C++ problems. Code output isn't right... Have no idea. Below is the problem: Below is my code: bool isLeapYear(int year) { if (year % 400
C++ problems. Code output isn't right... Have no idea.
Below is the problem:
Below is my code:
bool isLeapYear(int year) { if (year % 400 == 0) { return true; } else { return false; } }
int getCenturyValue(int year) { int century = year / 100; int r = 3 - (century % 4); return r * 2; }
int getYearValue(int year) { int lastTwoDigit = year % 100; int v = lastTwoDigit / 4; return v + lastTwoDigit; }
int getMonthValue(int month, int year) { switch (month) { case1: if (isLeapYear(year)) { return 6; } else { return 0; } case2: if (isLeapYear(year)) { return 2; } else { return 3; } case3: return 3; case4: return 6; case5: return 1; case6: return 4; case7: return 6; case8: return 2; case9: return 5; case10: return 0; case11: return 3; case12: return 5; } }
int main(){
//precondition: input dates. eg:July 4.2008 //postcondition: output the day of the week correspond to that date string months; int month; int date; int year; int day; string dayOfWeek; cout > months >> date >> year; if (months == "January") { month = 1; } else if (months == "Febuary") { month = 2; } else if (months == "March") { month = 3; } else if (months == "April") { month = 4; } else if (months == "May") { month = 5; } else if (months == "June") { month = 6; } else if (months == "July") { month = 7; } else if (months == "August") { month = 8; } else if (months == "September") { month = 9; } else if (months == "October") { month = 10; } else if (months == "November") { month = 11; } else if (months == "December") { month = 12; } else { cout
}
My output is like below:
It can't print out the day of the week properly.
I don't know which step I got wrong??
Could anyone help me out??
So many thanks!!!!
12. Write a program that inputs a date (e-g., July 4, 2008) and outputs the day of the week that corresponds to that date. The following algorithm is from http:// en.wikipedia.org/wiki/Calculating_the_day_of_the_week. The implementation will require several functions: bool isleapYear (int year); This function should return true if year is a leap year and false if it is not. Here is pseudocode to determine a leap year: leap year=((year divisible by 400) or (year divisible by 4 and year not divisible by 100)) int getCenturyvalue (int year); This function should take the first two digits of the year (i.e., the century), divide by 4, and save the remainder. Subtract the remainder from 3 and return this value multiplied by 2. For example, the year 2008 becomes (20/4)= 5 remainder 0.3-0=3. Return 3 * 2=6. int getYearValue(int year); This function computes a value based on the years since the beginning of the century. First, extract the last two digits of the year. For example, 08 is extracted for 2008. Next, factor in leap years. Divide the value from the previous step by 4 and discard the remainder. Add the two results together and return this value. For example, from 2008 we extract 08. Then (8/4)= 2 remainder 0. Return 2+8=10. int get Monthvalue(int month, int year); This function should return a value based on the following table and will require invoking the isLeapYear function: MONTH RETURN VALUE January 016 if year is a leap year) February 3 (2 if year is a leap year) March 3 April May 6 1 June July 6 August 2 September October 5 0 November 3 December 5 Finally, to compute the day of the week, compute the sum of the date's day plus the values returned by getMonthvalue, getYearvalue, and get Centuryvalue. Divide the sum by 7 and compute the remainder. A remainder of corresponds to Sunday, 1 corresponds to Monday, etc.up to 6which corresponds to Saturday. For example, the date July 4, 2008 should be computed as (day of month) + (get Monthvalue) + (getYearvalue) + (get Centuryvalue) = 4 + 6 + 10+ 6 = 26. 26/7 = 3 remainder 5. The fifth day of the week corresponds to Friday. Your program should allow the user to enter any date and output the correspond- ing day of the week in English. Please type the date in Month(in English) day (in number) year(in number), for example July 4 2008. Febuary E19 -2020 The day of the week of this date is: 6 C:\Users\Wenhui Fan source AbsoluteC++ lexercise03\x64\Debug exercise03.exe (process 19864) exited with code @.! Press any key to close this windowStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started