help with a simple function

Function int readHours() reads one or more hours worked by an employee in the week, computes their sum and returns it. The list of hours is terminated by the dummy value -99. Function main calls the function 3 times with input values and then prints the total number of hours

Im not getting a value returned after inputting a value, anyone see any mistakes ?

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
  #include<iostream>
using namespace std;
#define DUMMYVALUE -99
int readhours()
{
	int sum=0, hours;
	cin>>hours;
	while(hours != DUMMYVALUE);
	{
		sum+=hours;
		cin>>hours;	
	}
	return sum;
}
int main ()
{
	cout<<endl << "Number of hours:\t";
	cout<< readhours();
	cout<<endl<< "Number of hours:\t";
	cout<< readhours();
	cout<<endl<< "Number of hours:\t";
	cout<<readhours();
	return 0;
}
closed account (48T7M4Gy)
line 8 has a problem with its colon. Surgery is required.

Also you don't need to duplicate all those lines after line 19.
Last edited on
surgerey ? What is surgerey, and yes I do need to duplicate those lines, since it is calling the function 3 times.
Omg, I see my error, thank you. you could have just mentioned the semi colon in line 8, lmfao.
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>

using namespace std;

#define DUMMYVALUE -99

int readhours()
{
	int sum = 0, hours = 0;
	
	while( hours != DUMMYVALUE )
	{
	    sum += hours;
	    cout << "Number of hours: ";
		cin >> hours;
	}
	return sum;
}

int main ()
{
    cout<< readhours();	
	return 0;
}
closed account (48T7M4Gy)
Surgeon fixed up surgery error too - multiskilled.

I took the liberty of showing you how you could simplify a bit. But keep in mind yours is OK. :)
closed account (48T7M4Gy)
PS I know you were calling the function three times and maybe you have special reason but remember that the dummy value allows any number of inputs, so a user would get sick of typing in -99. There are also problems if you wanted a grand total of the three.
Topic archived. No new replies allowed.