Need help with c++ language. Can someone help me with my code? It converts numbe
ID: 3569360 • Letter: N
Question
Need help with c++ language.
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. I do not want the program to convert 0 to January 0. Entering 0 should end the program, not convert it to January 0 and then end. Please update my code below and do not post a completely different code. 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
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;
}
//-----//
2)
//-----//
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){
//Send entered day number to the print() function
dYear.print(day);
}
}while( day != 0);
return 0;
}
//-----//
3)
//--------//
int main()
{
DayOfYear dYear;
//Set days of each month into an array
int day;
//Ask user the total day number
while(1)
{
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);
}
return 0;
}
//-----//
4)
//-----//
int main()
{
DayOfYear dYear;
//Set days of each month into an array
int day;
//Ask user the total day number
cout << " Enter a number you would like to convert into a month and day";
cin >> day;
while(day!=0)
{
//Send entered day number to the print() function
dYear.print(day);
cout << " Enter a number you would like to convert into a month and day";
cin >> day;
}
return 0;
}
//-----//