what to return on a struct function

Hello, I'm having some trouble with my homework assignment that involves structs. I have the structure here:

1
2
3
4
5
6
7
8
9
struct change {
	change ();

	int quarters = 25;
	int dimes = 10;
	int nickels = 5;
	int pennies = 1;

};


The problem however, is that I was instructed to add a function:
 
change ComputeChange(int cents)


I wrote the function(see below), but I don't know what to return. I've tried 0 and 1, cents, void, change, and chng.quarters. Here is the rest of the function:
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
change ComputeChange(int cents) {
	change chng;
	int valQuarter;
	int valDime;
	int valNickel;

	chng.quarters = cents % 100 / 25;
	valQuarter = cents % 100 / 25 * 25;
	cents = cents - valQuarter;

	chng.dimes = cents % 100 / 10;
	valDime = cents % 100 / 10 * 10;
	cents = cents - valDime;

	chng.nickels = cents % 100 / 5;
	valNickel = cents % 100 / 5 * 5;
	cents = cents - valNickel;

	chng.pennies = cents % 100 / 1;

	cout << "quarters: " << chng.quarters << std::endl;
	cout << "dimes: " << chng.dimes << std::endl;
	cout << "nickels: " << chng.nickels << std::endl;
	cout << "pennies: " << chng.pennies << std::endl;

	return ;
}
You want to return something of type change, right? The only object of type change in your function is chng, so try return chng;
Well that did remove the error I was getting, but now I'm getting an error saying I have an unresolved external.

Here's all of my code in case it helps:
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
#include <iostream>
using namespace std;

struct change {
	change ();

	int quarters = 25;
	int dimes = 10;
	int nickels = 5;
	int pennies = 1;

};

change ComputeChange(int cents) {
	change chng;
	int valQuarter;
	int valDime;
	int valNickel;

	chng.quarters = cents % 100 / 25;
	valQuarter = cents % 100 / 25 * 25;
	cents = cents - valQuarter;

	chng.dimes = cents % 100 / 10;
	valDime = cents % 100 / 10 * 10;
	cents = cents - valDime;

	chng.nickels = cents % 100 / 5;
	valNickel = cents % 100 / 5 * 5;
	cents = cents - valNickel;

	chng.pennies = cents % 100 / 1;

	cout << "quarters: " << chng.quarters << std::endl;
	cout << "dimes: " << chng.dimes << std::endl;
	cout << "nickels: " << chng.nickels << std::endl;
	cout << "pennies: " << chng.pennies << std::endl;

	return chng;
}

void PrintChange(change c) {
	int valQuarters = 25;
	int valDimes = 10;
	int valNickels = 5;
	int valPennies = 1;

	cout << "quarters: " << valQuarters << std::endl;
	cout << "dimes: " << valDimes << std::endl;
	cout << "nickels: " << valNickels << std::endl;
	cout << "pennies: " << valPennies << std::endl;

return;
}
int main();
int main(){
	int cents;

	cents = 67;
	ComputeChange(67);


	cents = 26;
	ComputeChange(26);


	cents = 79;
	ComputeChange(79);


	system("PAUSE");

	return 0;
}
On line 5 you tell the compiler "Hey this exists and I promise to define it later" and then you break your promise by never defining it.
I see. I'm not really sure how or where I should define this. I tried to define it by copying the line to the line below and putting the variables inside the parentheses, but I still get the same error.
///////////////////////////
I just removed the constructor and it runs perfectly. Thank you.
Last edited on
Topic archived. No new replies allowed.