Error in print function's display

I can not figure out why the print function will not print the month and day correctly in this program. The display when it is called is -858993460 for no matter what number is entered by the user. Pasted below is the class header file I created and the .cpp with the function definitions. The main program just asks for an int from the user, and given to the class as an argument. Then the classname.print() is used to call the print function. Can anyone help me figure this one out? Thanks in advance.

#ifndef DayOfYear_H
#define DayOfYear_H
#include <string>
#include <iostream>

using namespace std;


class DayOfYear
{
private:

int number; //To hold number entered by user
string month; //To hold array for name of month
int day; //To hold the day of the month

public:

DayOfYear(int num)
{
//Check for day selected to be the correct domain
if (num <= 0 || num > 365)
{
cout << "\nInvalid selection, please choose a day ";
cout << "between 1 and 365.";
}
else
number = num;

}

void setmonth(string mon)
{ month = mon; }

void setday(int d)
{ day = d; }

//function to determine month of day chosen
static string getmonth(int number); //defined in DayOfYear.cpp

//function to determine the day of the month chosen
int getdayofmonth(int number); //defined in DayOfYear.cpp

//print function
void print() //To display the formatted month and day
{
cout << "The month and day selected is:\t\t";
cout << month << " " << day << "\n";
}
};


#endif


.cpp file below

#include "DayOfYear.h"
#include <string>

//Determine what month day selected fall in
string DayOfYear::getmonth(int number)
{
int n = number;
string month;

if (n <= 31)
month = "January";
else if (n <= 59)
month = "February";
else if (n <= 90)
month = "March";
else if (n <= 120)
month = "April";
else if (n <= 151)
month = "May";
else if (n <= 181)
month = "June";
else if (n <= 212)
month = "July";
else if (n <= 243)
month = "August";
else if (n <= 273)
month = "September";
else if (n <= 304)
month = "October";
else if (n <= 334)
month = "November";
else if (n <= 365)
month = "December";

return month;
}

//Determine what day of the month the day selected is
int DayOfYear::getdayofmonth(int number)
{
static int day;

int n = number;

if (n <= 31)
day = n;
else if (n <= 59)
day = n - 31;
else if (n <= 90)
day = n - 59;
else if (n <= 120)
day = n - 90;
else if (n <= 151)
day = n - 120;
else if (n <= 181)
day = n - 151;
else if (n <= 212)
day = n - 181;
else if (n <= 243)
day = n - 212;
else if (n <= 273)
day = n - 243;
else if (n <= 304)
day = n - 273;
else if (n <= 334)
day = n - 304;
else if (n <= 365)
day = n - 334;

return day;
}
post your main().
getmonth and getdayofmonth could be done better:
1
2
3
4
5
6
7
8
9
10
//a combination of the two
const int num_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};//deal with February..
const char* month_name[] = {"January", "February", "March", "April", "May", "June",
             "July", "August", "September", "October", "November", "December"};

int day = 153;//form 1 to 365. a copy of the original input.
int month;
for(month = 0; day > num_days[month]; day-=num_days[month], month++);

std::cout << month_name[month] << " " << day;
Topic archived. No new replies allowed.