identifier not found?

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
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
using namespace std;


int main()
{
double rate;
double hours;
double amount;


amount = getSalary(hours, rate);

cout << " The total salary is: " << amount << endl;


}

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

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

double getSalary (double hours, double rate)
{
double amount;

if (hours > 40)
{
amount = 40 * (rate);
amount += (hours - 40) * (rate * 1.5);
}
else
{
amount = hours * rate;
}
return amount;
}


This is the code I wrote but it says identifier not found for getSalary on line 12. I have a bad teacher so I kind of don't know what to do to fix this problem? please help me out? and if there are other problems please let me know thanks.

ps. also do i need a return0; on line 16?
Last edited on
At the time the compiler parses line 12, it hasn't seen the definition for getSalary yet, which is why you get the undefined message.

You have two choices:
1) Move getSalary (lines 37-51) before main.
2) Create a forward declaration for getSalary and place it before main. The forward declaration of getSalary must match the implementation exactly.
 
double getSalary (double hours, double rate);

Which option you choose is mostly a matter of style.

Other issues:
1) Line 12: hours and rate are uninitialized. You need to call getHours() and getRate().
2) You have the same forward declaration issue with getHours() and getRate().


Yes, your should have a return 0; at line 16.
The compiler doesn't know what getSalary is on line 12 because it has not been declared yet. The solution is to declare the function before main
double getSalary(double, double);
or put the whole definition of getSalary before main.

Normal functions with return type other than void should always have a return statement, but main is an exception to this rule. If you leave it out it will return 0.
Last edited on
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
using namespace std;

double getSalary (double rate, double hours);
double getHours (double hours); /* added this because it said getHours identifier not found */
double getRate (double rate); // same reason as above

int main()
{
double rate;
double hours;
double amount;

getHours();

getRate();

amount = getSalary(hours, rate);

cout << " The total salary is: " << amount << endl;

return 0;
}

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

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

double getSalary (double hours, double rate)
{
double amount;

if (hours > 40)
{
amount = 40 * (rate);
amount += (hours - 40) * (rate * 1.5);
}
else
{
amount = hours * rate;
}
return amount;
}


now the error is 'getHours doesn't take 0 arguments'
and getRate doesn't take 0 arguments.

:/ what should I do? and also rate and hours below line 43 are in grey? which suggests that something might be wrong there
I said before that the forward dseclaration must match the implementation exactly.
Your forward declarations of getHours and getRate specify that each accepts one double argument. The implementation of getHours and getRate take no arguments, hence the compiler telling you they don't match.
Ok I think I'm close to getting it. You are saying that in lines 5 and 6 (those are my forward declarations?) specify that they accept 1 double argument; and the getHours and getRate in lines 14 and 16 take no arguments?
What can I do to fix that?

You are saying that in lines 5 and 6 (those are my forward declarations?) specify that they accept 1 double argument; and the getHours and getRate in lines 14 and 16 take no arguments?

Correct.


What can I do to fix that?

Make them the same?

Hint: Since getRate() at line 34 takes no arguments. Make the forward declaration at line 6 agree.

One other problem. At lines 14 and 16, you're not assigning the returned value from the function to your local variables.


Make them the same?

I did that now. It tells me "run-time check failure #3: the variable rate is being used without being initialized." It talks about the rate on line 18. What is the problem with that? I thought i initialized both hours and rate ?

Since getRate() at line 34 takes no arguments. Make the forward declaration at line 6 agree.

What do you mean by make it agree? like instead of writing double getRate() in line 34 i should write double getRate (double rate) ??

One other problem. At lines 14 and 16, you're not assigning the returned value from the function to your local variables.

I don't get what that means sorry.

I did that now. It tells me "run-time check failure #3: the variable rate is being used without being initialized." It talks about the rate on line 18. What is the problem with that? I thought i initialized both hours and rate ?

You create rate in main and pass it to getSalary where it is used without being initialized (assigned a value).

What do you mean by make it agree? like instead of writing double getRate() in line 34 i should write double getRate (double rate) ??

No, the other way. Make both double getRate(), because you don't want getRate to take arguments.

I don't get what that means sorry.

You are calling the function but you don't store the return value anywhere. rate and hours in main are not the same variables as the ones in getHours() and getRate(). Maybe this is what you want to do
1
2
hours = getHours();
rate = getRate();
This would fix the problem with the "run-time check failure #3".
Last edited on
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
using namespace std;

double getSalary (double rate, double hours);
double getHours ();
double getRate ();


int main()
{
double rate;
double hours;
double amount;

hours = getHours();

rate = getRate();

amount = getSalary(hours, rate);

cout << " The total salary is: " << amount << endl;

return 0;
}

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

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

double getSalary (double hours, double rate)
{
double amount;

if (hours <= 40)
{
	amount = rate * hours;
}
else if (hours > 40) 
{
	amount = hours * (rate * 1.5);
}
return amount;
}


I did everything you guys told me to do and the program works. When I put >40 hours worked I get the amount of money I'm suppsoed to get, when I put <40 I get the amount I'm supposed to get. The only problem is when I put 40 hours. It tells me 'run time check failure #3: the variable amount is being used without being initialized.'
It executes correctly for me using VS2010 with 39, 40 and 41 hours.

BTW, I believe your overtime calculation is incorrect.
You're calculating amount based on 1.5x the rate for the entire hours worked.
Normally, overtime is paid only for the hours in excess of 40.

BTW, I believe your overtime calculation is incorrect.
You're calculating amount based on 1.5x the rate for the entire hours worked.
Normally, overtime is paid only for the hours in excess of 40.


It executed flawlessly after I changed it so that only the overtime gets payed * 1.5...
Thanks you so much you have been very helpful, by giving me hints and making me learn and figure out the program step by step.
I wish one day to be able to pay you back.
Last edited on
Topic archived. No new replies allowed.