Need help with homework problem

Solved
Last edited on
In getSalary() function you want to use hours variable that doesn't have value, 'cuz you didn't assign any to it.
I'm confused >_<
1
2
3
double hours;
double rate;
amount = hours * rate;


you have not initialised hours or rate. They will contain completely garbage results. So amount will therefore contain garbage too.
How can I get the hours and rate values to carry over from the previous functions?
pass by reference.
http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/

edit: but you also need to sort out your logic of what you are passing to what.
e.g.:
1
2
3
4
5
6
void getSalary (double amount)
{
double hours;
double rate;
amount = hours * rate;
..

You are passing in 'amount', so why the hell try and calculate it? Unless you aren't passing in amount, then you should call it something else.
stuff like that.
Last edited on
Okay, pass by reference. I just can't understand this..
I don't want to ask somebody to do my homework for me, so could somebody just apply this to the first functions on my code as an example?
I really appreciate it.
something like this;
1
2
3
4
5
void getHours (double& hours){
cout << " Enter the hours worked. " << endl;
cin >> hours;
cout << endl;
}

then you call it this way;
1
2
double hours;
getHours(hours);

If you didn't have reference yet you should do it like so:
1
2
3
4
5
6
7
double getHours (){
double hours;
cout << " Enter the hours worked. " << endl;
cin >> hours;
cout << endl;
return hours;
}

Then you call it:
double hours = getHours();
Last edited on
Hey Hardew, thanks for the response, but it's not working for me.
Could you paste it into the whole code and then I could see if I still get the same errors?
Is this want you wanted to do?
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
38
#include <iostream>
using namespace std;

void getHours(double&);
void getRate(double&);
void getSalary(double, double); // if it's your payCheck() function, it should return value according to your first post

int main() {
	double rate;
	double hours;

	getHours(hours);
	getRate(rate);
	getSalary(hours, rate);
	cin.get();
	cin.get();
	return 0;
}

void getHours(double& hours) {
	cout << " Enter the hours worked. " << endl;
	cin >> hours;
	cout << endl;
}

void getRate(double& rate) {
	cout << " Enter the rate per hour at which you get paid." << endl;
	cin >> rate;
	cout << endl;
}

void getSalary(double hours, double rate) {
	double amount;
	amount = hours * rate;
	if (hours > 40)
		amount = hours * (rate * 1.5); // braces not needed
	cout << " The total salary is: " << amount << endl;
}
Last edited on
That's working.. Thanks so much hardew, I will review this, my coding definitely needs work. :P
can you put your original code back please?
Topic archived. No new replies allowed.