Need HELP ASAP! C++ language only. Can someone help me with my code? It converts
ID: 3569359 • Letter: N
Question
Need HELP ASAP! C++ language only.
Can someone help me with my code? It converts numbers up to 365 into month day format. The problem is when I enter 0 it converts it into January 0. It should not convert 0 to January 0. Entering 0 should make the program end but it converts it to January 0 first, then it ends. Please help update the code below, and do not post a different code please. Thank you very much.
Here is my code:
#include <iostream>
#include <string>
using namespace std;
//Day of the year class declaration
class DayOfYear
{
private:
public:
void print(int);
};
static const int MonthDays[] = {31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
//Set the name of each month into an array
static const string MonthName[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
//**************************************************
// This function displays the month and day using *
// the number entered. *
//**************************************************
void DayOfYear::print(int day)
{
int month = 0;
while (MonthDays[month] < day)
month = (month + 1) %12;
//Display month and day
cout << MonthName[month] << " ";
if(month == 0)
cout<<day;
else
cout<< day - MonthDays[month-1];
}
int main()
{
DayOfYear dYear;
//Set days of each month into an array
int day;
//Ask user the total day number
do
{cout << " Enter a number you would like to convert into a month and day";
cin >> day;
//Send entered day number to the print() function
dYear.print(day);
}while( day != 0);
return 0;
}
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
//Day of the year class declaration
class DayOfYear
{
private:
public:
void print(int);
};
static const int MonthDays[] = {31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
//Set the name of each month into an array
static const string MonthName[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
//**************************************************
// This function displays the month and day using *
// the number entered. *
//**************************************************
void DayOfYear::print(int day)
{
int month = 0;
while (MonthDays[month] < day)
month = (month + 1) %12;
//Display month and day
cout << MonthName[month] << " ";
if(month == 0)
cout<<day;
else
cout<< day - MonthDays[month-1];
}
int main()
{
DayOfYear dYear;
//Set days of each month into an array
int day;
//Ask user the total day number
do
{cout << " Enter a number you would like to convert into a month and day";
cin >> day;
if(day == 0){
break;
}
//Send entered day number to the print() function
dYear.print(day);
}while( day != 0);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.