Input: two integer numbers, the first in the range 1..12. The second one greater than 1899. If the
user enters numbers outside those ranges, the program should prompt the user again until the user
enters correct data. The first number represents a month, the second number represents a year.
Output: number of days in the month for that year.
Important: After data is entered once and answer produced, program should prompt user for continuation
of execution as follows:
If you want to continue execution enter 1, or any other number to terminate.
Hence program should continue executing until program is terminated by user.
THIS IS WHAT I GOT SO FAR i really dont know how to put my main in order
#include <iostream>
#include <cmath>
using namespace std;
/*Instructions to use program*/
void welcome () {
cout<< endl;
cout<<" This program returns the number of days in a given year " <<endl;
cout<<" and month considering february as lepear or not for each year " <<endl;
cout<<endl;
}
int readMonth(){
int month;
month=-1;
while( month > 0 || month < 13){
cout << "Please enter an integer number between 1..12:" << endl;
cin >> month;
}
return month;
}
int readYear(){
int year ;
year=-1 ;
while ( year > 1899 ) {
cout << "Please enter an integer number gretaer than 1899" << endl;
cin >> year ;
}
return year;
}
bool isLeap(int year, int month){
bool feb;
if(year%4==0 && year%100!=0 || year%400==0){
feb = 28;
}
else{
feb= 29;
}
return feb;
}
void display ( int month, int year , int feb ){
if (month==1)
cout << "January of "<< year << "has 31 days"<< endl;
else if (month==2)
cout << "February of "<< year << "has "<<feb<<"days"<< endl ;
else if (month==3)
cout << "March of "<< year << "has 31 days"<< endl;
else if (month==4)
cout << "April of "<< year << "has 30 days"<< endl;
else if (month==5)
cout << "May of "<< year << "has 31 days"<< endl;
else if (month==6)
cout << "June of "<< year << "has 30 days"<< endl;
else if (month==7)
cout << "July of "<< year << "has 31 days"<< endl;
else if (month==8)
cout << "August of "<< year << "has 31 days"<< endl;
else if (month==9)
cout << "September of "<< year << "has 31 days"<< endl;
else if (month==10)
cout << "October of "<< year << "has 31 days"<< endl;
else if (month==11)
cout << "November of "<< year << "has 30 days"<< endl;
else if (month==12)
cout << "December of "<< year << "has 31 days"<< endl;
}
bool tryAgain(){
int answer;
cout << endl << "Do you want to continue? ";
cout << "if you want to continou execution enter 1 , or any number to terminate" << endl;
cin >> answer;
return answer == 1;
}
int main(){
int month(0);
int year (0);
bool feb;
bool answer;
welcome();
answer = true;
while(answer == true){
month = readMonth() ;
year = readYear ();
feb = isLeap() ;
eg, readMonth - while condition loops while input is valid, not invalid;
readYear - same problem as readMonth;
isLeap is declared to return a bool, but your code is trying to return the number of days in february instead.
In void display() I think is better if you use a switch instead of many ifs
Switch would take more lines of code. And I think compilers nowadays will compile then down to similar code anyways. So there isn't really alot of use in moving to a switch unless it's for readability.