In: Computer Science
(c++) error:
=================== MISMATCH FOUND ON LINE 0007: ===================
ACTUAL : 2~year
EXPECTED: 2~years
======================================================
#include <iostream>
using namespace std;
int main()
{
const int MonthDays[]= { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30
,31 };
const string MonthName[]=
{"jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"};
int day;
int year=0;
int index;
while(1)
{
cout<<"Please enter a day of the year (0 to exit): ";
cin>>day;
cout<<day<<endl;
if(day==0)
{
break;
}
index=0;
year=0;
while(day>365)
{
day=day-365;
year++;
}
if(year !=0)
{
cout<<year<<" year"<<endl;
}
index=0;
while(day>MonthDays[index])
{
day=day-MonthDays[index];
index++;
}
cout<<MonthName[index]<<"
"<<day<<endl;
}
cout<<"Thanks for playing!";
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!
I have answered this before. Your test program is expecting an output of ‘2 years’ for some input, but your program is giving ‘2 year’ (no s at the end). So I fixed your code to print years instead of year if n is not 1. Also your code had some misplaced statements. I made some rearrangements, now it runs perfectly as needed.
NOTE: Copy the entire code from below, and try running. DO NOT JUST CHANGE YEAR TO YEARS OR VICE VERSA. IT WONT WORK. Modified code is highlighted in bold text. Thanks
#include <iostream>
using namespace std;
int main()
{
int daynum, n;
cout << "Please enter a day of the year (0 to exit): ";
cin >> daynum;
while (daynum > 0) {
//checking if daynum>365 at the beginning of loop
if (daynum > 365) {
n = daynum / 365;
if(n==1){
//printing year instead of years for n=1
cout << n << " year" << endl;
}else{
//printing years instead of year for n>1
cout << n << " years" << endl;
}
//updating daynum, so that it will be under 365
daynum = daynum - (365 * n);
}
//now finding the day of year
if (daynum >= 1 && daynum <= 31)
cout << "jan " << daynum << endl;
else if (daynum > 31 && daynum <= 59) {
cout << "feb " << daynum - 31 << endl;
}
else if (daynum > 59 && daynum <= 90) {
cout << "mar " << daynum - 59 << endl;
}
else if (daynum > 90 && daynum <= 120) {
cout << "apr " << daynum - 90 << endl;
}
else if (daynum > 120 && daynum <= 151) {
cout << "may " << daynum - 120 << endl;
}
else if (daynum > 151 && daynum <= 181) {
cout << "jun " << daynum - 151 << endl;
}
else if (daynum > 181 && daynum <= 212) {
cout << "jul " << daynum - 181 << endl;
}
else if (daynum > 212 && daynum <= 243) {
cout << "aug " << daynum - 212 << endl;
}
else if (daynum > 243 && daynum <= 273) {
cout << "sep " << daynum - 243 << endl;
}
else if (daynum > 273 && daynum <= 304) {
cout << "oct " << daynum - 273 << endl;
}
else if (daynum > 304 && daynum <= 334) {
cout << "nov " << daynum - 304 << endl;
}
else if (daynum > 334 && daynum <= 365) {
cout << "dec " << daynum - 334 << endl;
}
cout << "Please enter a day of the year (0 to exit): ";
cin >> daynum;
}
if (daynum == 0)
cout << "Thanks for playing!";
if (daynum < 0)
cout << "Invalid input!";
return 0;
}
//OUTPUT
Please enter a day of the year (0 to exit): 1
jan 1
Please enter a day of the year (0 to exit): 2
jan 2
Please enter a day of the year (0 to exit): 300
oct 27
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): 789
2 years
feb 28
Please enter a day of the year (0 to exit): 10000
27 years
may 25
Please enter a day of the year (0 to exit): 0
Thanks for playing!