Hello i been doing some program of my own, my program is to get the number of days paased in the year. I am having hard to get get the passed in the year. Now when i try to run and the output gives me "4438232", i am really having hard time to get this right..

For example if the user enters (mm-dd-yy) 3-18-2013, then the reamaing days passed in the year is 77.

`

``````  void dateType::Num_DayPassed()
{
int sum;
int yy = 365;

if (month ==1)
{
cout<<"Number of days Passed in the Year: "<<sum<<endl;
day=31;
sum=day-yy;
}
else if (month==2)
{
cout<<"Number of days Passed in the Year: "<<sum<<endl;
day=60;
sum=day-yy;
}
else if (month==3)
{
cout<<"Number of days Passed in the Year: "<<sum<<endl;
day=91;
sum=day-yy;
}
else if (month==4)
{
cout<<"Number of days Passed in the Year: "<<sum<<endl;
day=121;
sum=day-yy;
}
else if (month==5)
{
cout<<"Number of days Passed in the Year: "<<sum<<endl;
day=152;
sum=day-yy;
}
else if (month==6)
{
cout<<"Number of days Passed in the Year: "<<sum<<endl;
day=182;
sum=day-yy;
}
else if (month==7)
{
cout<<"Number of days Passed in the Year: "<<sum<<endl;
day=213;
sum=day-yy;
}
else if (month==8)
{
cout<<"Number of days Passed in the Year: "<<sum<<endl;
day=244;
sum=day-yy;
}
else if (month==9)
{
cout<<"Number of days Passed in the Year: "<<sum<<endl;
day=274;
sum=day-yy;
}
else if (month==10)
{
cout<<"Number of days Passed in the Year: "<<sum<<endl;
day=305;
sum=day-yy;
}
else if (month==11)
{
cout<<"Number of days Passed in the Year: "<<sum<<endl;
day=335;
sum=day-yy;
}
else if (month==12)
{
cout<<"Number of days Passed in the Year: "<<sum<<endl;
day=366;
sum=day-yy;
}

} ``````

output

`

6
Contributors
8
Replies
52
Views
11 Months
Discussion Span
Last Post by Johan_1

You have to somehow format your date value.

like how?

There are more basic problems with this code.
eg

``````             if (month ==1)
{
cout<<"Number of days Passed in the Year: "<<sum<<endl;
day=31;
sum=day-yy;
}``````

This displays `sum`, which has not been calculated yet
It then sets `day` and `sum`, but never uses those values.

I am confused with what you are trying to do because your question says you want then number of days REMAINING in the year yet your sample shows days PASSED.

I didn't walk through all of the code you posted but here are a few thoughts on what I'm seeing.

1) As pointed out earlier, you are displaying the value of sum before you calculate it. My guess is you are getting an "uninitialized" value.
2) You are subtracting the year (365) from the day (31, 60, 91, etc.) which is going to give you a negative number in sum. I'm thinking what you really wanted was days - day of month not days - year (by the way, you are using the variable "day" for two different purposes, screen input and calculation and that is a bad idea. Whoever has to support your code later will HATE you for it.).
3) It doesn't look like you are accounting for leap years where every value from month 2 on would be increamented by 1.

Instead of using that big ugly if/else statement, I would put the days in an array and use the month as the index (but maybe you aren't at that point in learning yet). I would also take the rest of the logic (sum calculation and display) out of the if/else and put it at the end so it's only there once.

Hope that helps.

Since there seems top be some confusion on your part, I would suggest a bit of refactoring. By making Month a nested class with members, name and days, You can simplify several parts of your code by storing the different months and their respective days in an array. It's a very simple matter to take the month number and subtract 1 to get the right index in the array. This simplifies a number of areas in your code:

``````class dateType
{
public:
dateType();
~dateType();
bool isLeapYear();
void setDate();
void setMonth( int m );
string getMonth();
void setDay( int d );
int getDay();
void setYear( int y );
int getYear();
void Num_Month();
void Num_DayPassed();
void Num_RemainingYear();
void printdate();

private:
class Month
{
public:
string name = "January";
int days = 31;
Month()
{

}
Month( string _name , int _days = 31 )
{
name = _name;
days = _days;
}
Month( const Month& other )
{
name = other.name;
days = other.days;
}

};
array<Month,12> months =
{
Month("January"),
Month("February",28),
Month("March"),
Month("April",30),
Month("May"),
Month("June",30),
Month("July",31),
Month("August"),
Month("September",30),
Month("October"),
Month("November",30),
Month("December")
};
Month month;
int day;
int year;
};

void dateType::setMonth( int m )
{
month = months[m];
}
string dateType::getMonth()
{
return month.name;
}
void dateType::Num_Month()
{
cout << "Number of days in a month: " << month.days << endl;
}
//Days passes is easily calculated with a loop that stops when month is found
void dateType::Num_DayPassed()
{
int sum;

for ( int i = 0; i < 12; i++ )
{
if ( months[i].name == month.name )
{
cout << "Number of days Passed in the Year: " << sum + months[i].days << '\n';
return;
}
sum += months[i].days;
}
}
//The remaining days is days from this month forward
void dateType::Num_RemainingYear()
{
int yy = 365;
int sum = 0;
bool found = false;
for ( int i = 0; i < 12; i++ )
{
if ( found )
{
sum += months[i].days;
}
if ( months[i].name == month.name )
{
found = true;
}
}
cout << "Number of days Remaining in the Year: " << sum << '\n';
}
void dateType::printdate()
{
cout << "Month: " << month.name << endl;
cout << "Date: " << day << endl;
cout << "Year: " << year << endl;

}``````

On a side note, the `Checkdate` function shouldn't be there. You should be verifying the date when the data is entered. Also, unless you have a need to fulsh the stream it is very inefficient to use `endl`. It is preferred for general use use the new line character, `'\n'`:

``````void dateType::setDate()
{
int monthIndex = -1;
while ( monthIndex < 1 || monthIndex > 12 )
{
cout << "Enter Month: ";
cin >> monthIndex;
}
month = Month( months[monthIndex-1] );
cout << '\n';
while ( day < 1 || day > month.days )
{
cout << "Enter Date: ";
cin >> day;
}
cout << '\n';
while ( year < 1900 || year > 3000 )
{
cout << "Enter Year: ";
cin >> year;
}
cout << '\n';
}``````

Edited by tinstaafl

``````int DaysInMonth(int year, int month)
{
if (month == 4 || month == 6 || month == 9 || month == 11)
return 30;
else if (month == 2)
return ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) ? 29 : 28;
else
return 31;
}

int GetDayOfYear(int year, int month, int day)
{
int days = 0;
for (int m=1; m<month; m++)
days += DaysInMonth(year, m);
return days + day;
}

int GetDaysRemaining(int year, int month, int day)
{
int full_year = 0;
for (int m=1; m<=12; m++)
full_year += DaysInMonth(year, m);
return full_year - GetDayOfYear(year, month, day);
}``````
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.