NumDays Class

I have a program here to which i have to represent a certain number of work hours into how many days of work that will be. 8 hours is 1 work day.

Here is the program:

#include <iostream>
# include <string>
using namespace std;

class Numdays
{
private:
double days;
int hours;


public:

Numdays();
Numdays(int h);
void setHours(int);
void setDays(double);
int getHours();
double getDays();


double operator + (const Numdays a)
{
return hours + a.hours;
}
double operator - (const Numdays a)
{
return hours - a.hours;
}
Numdays operator ++ ()
{
++hours;
days = hours / 8;
return *this;
}
Numdays operator ++ (int)
{
Numdays temp(hours);
hours++;
days = (hours + 1) / 8;
return temp;
}
Numdays operator -- ()
{
--hours;
days = hours / 8;
return *this;
}
Numdays operator -- (int)
{
Numdays temp(hours);
hours--;
days = (hours - 1) / 8;
return temp;
}

};



Numdays::Numdays()

{
days = 0;
hours = 0;

}



Numdays::Numdays(int h)

{
hours = h;
days = 0;

}



void Numdays::setHours(int h)
{
hours = h;
days = h / 8;

}



void Numdays::setDays( double d)

{
if (hours >= 8)
{
days += 1;
}

}

int Numdays::getHours()
{
return hours;
}

double Numdays::getDays()
{
return days;

}





int main()

{

Numdays d(0);
int work;
cout << "Please type in a certain amount of hours to convert how much work in days is it:" << endl;
cin >> work;

d.setHours(work);
d.setDays(work);

cout << " THe number of hours you put is" << d.getHours() << endl;
cout << " That would mean you work about " << d.getDays() << " days " << endl;

system("pause");
return 0;
}

The out put I am getting is:

EX:

Please type in a certain amount of hours to convert how much work in days is it:
8
The number of hours you put is 8
That would mean you work about 2 days
Press any key to continue . . .

EX:

Please type in a certain amount of hours to convert how much work in days is it:
12
THe number of hours you put is 12
That would mean you work about 2 days
Press any key to continue . . .

EX:

Please type in a certain amount of hours to convert how much work in days is it:
16
THe number of hours you put is 16
That would mean you work about 3 days
Press any key to continue . . .

can someone help me on why It has 1+ more day than it should be.

Also if someone can help me figure how it can give me answer like 1.50 hours.

Example How i put in 12 hours that is about 1.5 days of work

Can someone explain how I can make my class program give me this output.

Thanks guys!


1
2
3
4
5
6
7
void Numdays::setDays( double d)

{
if (hours >= 8)
{
days += 1;
}


So long as you've already set hours to 8 or greater, then no matter what value you pass to this function you'll be incrementing days. Try entering 5 hours and you'll see what I mean.
Try basing you condition on the value passed in instead.

Also your pre-decrement and pre-increment operators calculate wrong. Instead of calculating days from original hours decremented or incremented by one, you actually calculate it on original hours decremented or incremented by 2.
So i fixed a little bit of the program and it still shows that 8 hours consist of 2 days. I was able to get to to say if I work 12 hours it now shows 2.5 days. But its still a day ahead of what it should be
Can you post your fix? And please use the code formatting tags.

For comparison:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>

using namespace std;

int main()
{
	constexpr int hpd = 8;
	int tot_hours = -1;
	cout << "How many hours?" << endl;
	cin >> tot_hours;
	
	int days = tot_hours / 8;
	int hours = tot_hours % 8;
	string pd = (days > 1) ? "days" : "day";
	string ph = (hours >1) ? "hours" : "hour";
	
	if (tot_hours < 8)
	{
		cout << tot_hours << " " << ph << "." << endl;
		return 0;
	}
	
	cout << tot_hours << " hours is " << days;
	if ((hpd / 2) == hours)
	{
		cout << ".5 " << pd << "." << endl;
	}
	else if (hours > 0)
	{
		cout << " " << pd << ", and " << hours << " " << ph << "." << endl;
	}
	else
	{
		cout << " " << pd << "." << endl;
	}
}
Topic archived. No new replies allowed.