My C++ problem is as follows:
Question
My C++ problem is as follows:
Given a number, calculate how many years into the future it is,
and what date. Assume no leap years.
For example:
Please enter a day of the year (0 to exit): 1
jan 1
Please enter a day of the year (0 to exit): 365
dec 31
Please enter a day of the year (0 to exit): 366
1 year
jan 1
Please enter a day of the year (0 to exit): 0
Thanks for playing!
I have entered the following code:
#include <iostream>
#include <string>
using namespace std;
int main() {
const int MonthDays[]={31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
const string MonthName[]={“jan”, “feb”, “march”, “apr”, “may”, “june”, “jul”, “aug”, “sep”, “oct”, “nov”, “dec”};
int day, year, index;
cout<< “Please enter a day of the year (0 to exit): “;
cin>>day;
cout<<day<<endl;
while(day>0) {
if(day>365) {
year=day/365;
day=day%365;
cout<<year<<” Year”<<endl;
}
day=day%365;
if(day==0) {
cout<<“dec 31″<<endl;
}
if(day>=1 && day<=31){
cout<<MonthName[index]<<” “<<day;
cout<<endl;
}
else {
cout<<MonthName[index]<< ” “<<day-MonthDays[index-1];
cout<<endl;
}
cout<<“Please enter a day of the year (0 to exit): “;
cin>>day;
cout<<day<<endl;
}
cout<<“Thanks for playing!”;
return 0;
}
Here is my feedback from the compiler:
==================== YOUR OUTPUT =====================
0001: Please~enter~a~day~of~the~year~(0~to~exit):~1
0002: jan~1
0003: Please~enter~a~day~of~the~year~(0~to~exit):~365
0004: dec~31
0005: jan~0
=================== MISMATCH FOUND ON LINE 0005: ===================
ACTUAL : jan~0
EXPECTED: Please~enter~a~day~of~the~year~(0~to~exit):~31
======================================================
Adjust your program and re-run to test.
Test 1 failed