(a) Greets the user and explains its purpose.
(b) Prompts the user to enter an input date from the keyboard.
(c) Calculates & displays what is the day number for that particular input date in the given input year (counting that 1st January is the day no1, 2nd January is the day no 2, etc.).
(d) Calculates and displays how many days are left to the end of that year.
(e) Displays the information about the correct finish of run, about the way of closing the screen (e.g. Press any key…), and then keeps the screen with results visible until the user presses a key.
Assumptions: (1) assume that your program is going to be used for years 1 AD to 9999 AD; (2) assume that in this entire period of time, the present calendar is used (i.e. was, and is going to be used).
How can this be done without the use of arrays and switch statements. Only counter and event controlled loops. Error messages should also be provided if incorrect values are entered.
I'm learning myself before I go to varsity next year. I've come across this as a solution thus far - but I don't understand arrays and switch statements that well yet.
int main(){
int day,month,year;
int dayNum,lastDayDec;
string input;
cout<<"Input a date and this will determine"<<endl;
cout<< "what day it is in the year.\n"<<endl;
cout << "Enter a date MM/DD/YYYY: ";
getline(cin,input);
stringstream(input.substr(0,input.find("/"))) >> month;
stringstream(input.substr(input.find("/")+1,input.find_last_of("/"))) >> day;
stringstream(input.substr(input.find_last_of("/")+1)) >> year;
//cin >> day >> month >> year;
if ( day < 1 || day > 31 )
cout << "INVALID: Please try again." << endl;
if ( leapyear(year) ){
cout << year << " is a leap year." << endl;
}
cout << "You have entered: " << month << "-" << day << "-" << year << endl;
dayNum = getDay(day, month, year);
cout << "The day number is " << dayNum << endl;
if (leapyear(year))
cout << "There are " << 366-dayNum << " days left in the year."<<endl;
else cout << "There are " << 365-dayNum << " days left in the year."<<endl;
cout<<"\nPress any key to exit"<<endl;
cin.ignore();
}
int getDay(int day, int month, int year){
int p_days=0;
month--;
switch (month){
case 12 :
p_days += 31;
case 11 :
p_days += 30;
case 10 :
p_days += 31;
case 9 :
p_days += 30;
case 8 :
p_days += 31;
case 7 :
p_days += 31;
case 6 :
p_days += 30;
case 5 :
p_days += 31;
case 4 :
p_days += 30;
case 3 :
p_days += 31;
case 2 :
if (leapyear(year))
p_days += 29;
else
p_days += 28;
case 1 :
p_days += 31;
break;
default :
cout << "Invalid Value!";
}
p_days+=day;
return p_days;
}