???

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>

using namespace std;

void initialize(int &firstInt, int &secondInt, char &aChar);
void getHoursRate(double &hours, double &rate);
double payCheck(double hours, double rate);
void printCheck(double hours, double rate);
void funcOne(int &x, int &y);
void nextChar(char &aChar);

int main()
{
	int x, y;
	char z;
	double rate, hours;
	double amount;

	initialize(x, y, z);
	getHoursRate(hours, rate);
	amount = payCheck(hours, rate);
	cout << "Paycheck = " << amount << endl;
	funcOne(x, y);
	cout << "x = " << x << " and y = " << y << endl;
	nextChar(z);
	cout << "z = " << z << endl;

	return 0;
}

void initialize(int &firstInt, int &secondInt, char &aChar)
{
	firstInt = 0;
	secondInt = 0;
	aChar = ' ';
}

void getHoursRate(double &hours, double &rate)
{
	cout << "Please enter hours worked: ";
	cin >> hours;
	cout << "Please enter rate of pay: ";
	cin >> rate;
}

double payCheck(double hours, double rate)
{
	double theReturn;

	if (hours <= 40)
		theReturn = hours * rate;
	else
		theReturn = 1.5 * (hours - 40) * rate + 40 * rate;

	return theReturn;
}

void printCheck(double hours, double rate)
{
	cout << "Hours worked: " << hours << endl;
	cout << "Rate worked: " << rate << endl;
	cout << "Amount due: " << payCheck << endl;
}

void funcOne(int &x, int &y)
{
	int entNmbr;

	cout << "Please enter a number: ";
	cin >> entNmbr;
	x = 2 * x + y - entNmbr;
}

void nextChar(char &aChar)
{
	aChar = static_cast<char>(aChar + 1);
}


One thing... The variable payCheck on line 62 does not cause a compiling error. Why? Thank you!
Last edited on
because payCheck is the name of one of your functions.
I did not know that would work! So what does this mean exactly? Does this make the variable main assume a value of 0?
Last edited on
Probably a runtime error, or the address of the function.
This can certainly cause disorder in a program! How can I avoid it? What I mean to ask is when a function is defined, does a variable of the same name also initialize?
Last edited on
cout << "Amount due: " << payCheck(hours, rate) << endl;
Is there a problem with that line of code? I feel I'm missing something. Don't all user-defined functions need parantheses?
Last edited on
No variable is declared with the same name.

I am assuming you want to call the paycheck function to display the amount due.

cout << "Amount due: " << payCheck(hours, rate) << endl;

That will call payCheck(); and then display the results.
For
1
2
3
4
5
6
7
8
9
10
#include <iostream>

void function(){
	std::cout <<"!\n";
}

int main(){
	std::cout <<function;
	return 0;
}

I get
8: warning: the address of `void function()', will always evaluate as `true'
Consequently, the cout always prints 1. It doesn't make much sense to me.

EDIT: Damn. I took too long and now it seems out of place.
Last edited on
Truth is, I actually noticed this days after writing this code and now I don't even know how this came about... But why does it work? Can functions be used without parantheses?
No. But none of the code you've provided us with calls printCheck(); So it's never been run.
I just used printCheck and it works...

So why does it work? Why does payCheck run without error?
Last edited on
does it give you the right answer?
Yes.
Then it's likely your compiler. Certainly not standard behaviour, or behaviour I would rely on.

GCC will equate the function pointer to 1 and it'll print:

Hours worked: 10
Rate worked: 40
Amount due: 1
Last edited on
Yes, understood. Thank you...
What's GCC mean btw?
GCC is the name of the compiler. The most-standards complient, and multi-platform. The most dominant compiler on *Nix.

http://gcc.gnu.org/
helios:
you need to put std::cout << function();
instead of std::cout << function;
"function" is the address.
"function()" runs the program (and if it isn't void, sets the function call to the return val).
Topic archived. No new replies allowed.