In: Computer Science
Write a menu driven C++ program that prints the day number of the year , given the date in the form of month-day-year. For example , if the input is 1-1-2006 , then the day number is 1. If the input is 12-25- 2006 , the day number is 359. The program should check for a leap year. A year is leap if it is divisible by 4 but not divisible by 100. For example , 1992 , and 2008 are divisible by 4 and not divisible by 400. A year that is divisible by 100 is a leap year if it is also divisible by 400. For example , 1600 and 2000 are divisible by 400 , however , 1800 is not a leap year because 1800 is not divisible by 400. Program terminates when the user type in n or N. Fall 2020 – Introduction to C++ Programming Fall 2020 - Husain Gholoom – Senior Lecturer in Computer Science Page 2 Note : Your program must contain the following 6 functions : 1. A function that returns a Boolean value indicating whether or not the day entered is between 1 and 31. 2. A function that returns a Boolean value indicating whether or not the month entered is between 1 and 12. 3. A function that returns a Boolean value indicating whether or not the year entered is greater than 0 and less than or equal to current year. 4. A function that returns a Boolean value indicating whether or not the year entered is a leap year. 5. A function that returns a string value indicating the name of the month. 6. A function that returns a integer value indicating actual number of day that is equivalent to day, month, and year that is entered.
Rules : 1. Your program must compile and run using Code:Blocks version 17.12 under windows. 2. Your program must be documented according the style above . See the website for the sample programming style program. 3. You must use the appropriate libraries in writing this program. 4. You can not use any concept that was not discussed in the class such as arrays , global variables … etc . 5. Must use functions ( prototypes and definitions ) , repetitions , control structures and switch statements. 6. Must properly format the output by use the appropriate library. See the output below. Note that in the following 3 lines : Day Manipulation program by Husain Gholoom 11 / 02 / 2020 • Replace my first / last name with your own first / last name.
code:
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
bool isValidDay(int dataIn){
if(dataIn>=1 && dataIn<=31){
return true;
}else{
return false;
}
}
bool isValidMonth(int dataIn){
if(dataIn>=1 && dataIn<=12){
return true;
}else{
return false;
}
}
bool isValidYear(int dataIn){
if(dataIn>0 && dataIn<=2018){
return true;
}else{
return false;
}
}
bool isLeapYear(int year)
{
// If a year is multiple of 400,
// then it is a leap year
if (year % 400 == 0)
return true;
// Else If a year is muliplt of 100,
// then it is not a leap year
if (year % 100 == 0)
return false;
// Else If a year is muliplt of 4,
// then it is a leap year
if (year % 4 == 0)
return true;
return false;
}
string nameOfMonth(int month){
month--;
string months[] ={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
return months[month];
}
int numberOfDay(int day,int month,int year){
int numDaysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int count = 0;
for(int i=0;i<month-1;i++){
count+=numDaysInMonth[i];
}
count+=day;
if(isLeapYear(year) && month>2){
count++;
}
return count;
}
int main()
{
while(true){
string date;
cout<<"\nEnter the date : ";
cin>>date;
if(date.compare("n")==0 || date.compare("N")==0){
break;
}
string line;
stringstream ss(date); //stream to other variable
int day;
int month;
int year;
ss>>month>>day>>year;
day = -day; //- symbol read as negative so changing to positive
year = -year; //- symbol read as negative so changing to positive
if(!isValidDay(day)){
cout<<"Invalid day entered!!!"<<endl;
}else if(!isValidMonth(month)){
cout<<"Invalid month entered!!!"<<endl;
}else if(!isValidYear(year)){
cout<<"Invalid year entered!!!"<<endl;
}else{
int getDay = numberOfDay(day,month,year);
cout<<"Number of day is : "<<getDay;
}
}
return 0;
}