In: Computer Science
(c++) error:
=================== MISMATCH FOUND ON LINE 0002: ===================
ACTUAL : 0~years
EXPECTED: may~3
======================================================
#include <iostream>
#include <string>
using namespace std;
int main()
{
const int DAYS[]= { 31, 59, 90, 120, 151, 181, 212, 243, 273, 304,
334, 365 };
const string MONTHS[]=
{"jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"};
int day, month;
int totalYears=0;
while(true)
{
cout<< "Please enter a day of the year (0 to exit): ";
cin>>day;
cout<<day<<endl;
if(day == 0)
{
break;
}
else if (day<0)
{
cout<<"Invalid Input!";
}
else
{
if(day>365)
totalYears=day/365;
if(totalYears == 1)
{
cout<<totalYears<<" year";
cout<<endl;
}
else
{
cout<<totalYears<<" years";
cout<<endl;
}
}
day=day- (totalYears * 365);
for(int i=0; i<12; i++)
{
if(DAYS[i]>day-1)
{
cout<<MONTHS[i]<<" ";
if(day>31)
{
cout<<day - DAYS[i-1]<<endl;
}
else
{
cout<<day<<endl;
}
break;
}
}
}
cout<<"Thanks for playing!"<<endl;
return 0;
}
INSTRUCTIONS:
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!
Hi Student,
There is a minor mistake in the if else loop. You just need to change the below lines
else
{
if(day>365)
to
else if(day> 365) {
Complete code is given below for your referance
#include <iostream>
#include <string>
using namespace std;
int
main ()
{
const int DAYS[] =
{ 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
const string MONTHS[] =
{ "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep",
"oct",
"nov", "dec" };
int day, month;
int totalYears = 0;
while (true)
{
cout << "Please enter a day of the year (0 to exit): ";
cin >> day;
cout << day << endl;
if (day == 0)
{
break;
}
else if (day < 0)
{
cout << "Invalid Input!";
}
else if (day > 365)
{
totalYears = day / 365;
if (totalYears == 1)
{
cout << totalYears << " year";
cout << endl;
}
else
{
cout << totalYears << " years";
cout << endl;
}
}
day = day - (totalYears * 365);
for (int i = 0; i < 12; i++)
{
if (DAYS[i] > day - 1)
{
cout << MONTHS[i] << " ";
if (day > 31)
{
cout << day - DAYS[i - 1] << endl;
}
else
{
cout << day << endl;
}
break;
}
}
}
cout << "Thanks for playing!" << endl;
return 0;
}
I had checked the working of the program thoroughly.