My function is always returning 1 (true) instead of variable

Hi, I'm struggling with a c++ class and haven't been able to find out why this function I've created (onHand) is always returning the value "1" instead of what I want (which is an array value * an array value).

No matter what I try, each time onHand is called from main.cpp it displays "1".

Any tips would be greatly appreciated!

coin.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef COIN_H
#define COIN_H

class Coin
{
public:
	int Size;
	int Count;
	
	Coin();
};

extern Coin coinArray[5];
extern double onHand();

#endif 


coin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "coin.h"
#include <iostream>

Coin::Coin()
{
	Size = 0;
	Count = 0;
}


double onHand()
{
	double Total = 0;
	for(int i = 0; i < 5; i++)
		{
			coinArray[i].Count * coinArray[i].Size;
		}

	Total = Total / 100;

}


main.cpp
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
// Sean Freeman
// C++ Coin Machine Program
// 2013-2-1

#include <iostream>
#include "coin.h"

using namespace std;

int main()
{

Coin coinArray[5];

coinArray[0].Size = 100;
coinArray[1].Size = 25;
coinArray[2].Size = 10;
coinArray[3].Size = 5;
coinArray[4].Size = 1;
	
	cout << onHand << endl;

	cout << "Enter the number of Dollars | Quarters | Dimes | Nickels | Pennies" << endl;
	cin >> coinArray[0].Count >> coinArray[1].Count >> coinArray[2].Count >> coinArray[3].Count >> coinArray[4].Count;
	
	cout << "$" << onHand << " in machine." << endl;

	return 0;
}
I just realized the coin.cpp I posted contained incomplete code:

line 16 just has coinArray[i].Count * coinArray[i].Size

when I have Total += coinArray[i].Count * coinArray[i].Size instead, I get a compiler error about unidentified symbols for architecture x86_64 so I took it out just to get it compiling for now, but I don't understand why its always returning 1.
You aren't returning anything in onHand. need to rewrite it this way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "coin.h"
#include <iostream>

Coin::Coin()
{
	Size = 0;
	Count = 0;
}


double Coin::onHand()
{
	double Total = 0;
	for(int i = 0; i < 5; i++)
		{
			coinArray[i].Count * coinArray[i].Size;
		}

	Total = Total / 100;
        return Total;

}


And yes, as you pointed out, the statement inside the for loop will not do anything.
Last edited on
Thanks randisking, when I add return Total; it still prints "1" when I run it.
In the for loop of your function double onHand() you calculate the value for each coin but fail to add it to Total. The function is suppose to return a double value but there is no return statement. Add,

return total;

I am surprised that it compiles without a return statement.
Made some edits to the post above because you need to remember to include class name in class functions and I missed that at first (tired as hell). And yes, the line inside the for loop needs to be fixed as well but I'm not going to do it because it sounds like you are on the right track now.
Alrededor, when I have the for loop add to Total I get a compiler error about unidentified symbols, do you know why this would be?

My initial thought was Total = Total + coinArray[i].Count * coinArray[i].Size;

but it give the error above.
One other thing I just noticed. In main.cpp line 26:

cout << "$" << onHand << " in machine." << endl;

You are not actually calling the function. You need onHand() with parenthesis to call the function. Without the parenthesis onHand is a pointer to the function. Since you never actually call the function that may explain why it compiles. The linker never tries to link to the function itself because it is never used.
ah, ok, that makes sense! and changing onHand to onHand() does make it output 0.

as a test I went back and changed double Total = 0; to double Total = 4; and it output 0.04 instead of 0, so the for loop isn't changing Total?
Here is the compiler error when my for loops looks like:

1
2
3
4
5
6
7
8
9
10
11
12
double onHand()
{
	double Total = 0;
	for(int i = 0; i < 5; i++)
		{
			Total = Total + coinArray[i].Count * coinArray[i].Size;
		}

	Total = Total / 100;
	return Total;

}


Undefined symbols for architecture x86_64:
"_coinArray", referenced from:
onHand() in cc2i8MXk.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status


Here, instead of using a global, I would recommend having onHand take the array and its size as parameters.

The linker error you're receiving is because coinArray is in main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using namespace std;

Coin coinArray[5]; // <-- put coinArray here
int main()
{

Coin coinArray[5]; // <-- take coinArray out of here

coinArray[0].Size = 100;
coinArray[1].Size = 25;
coinArray[2].Size = 10;
coinArray[3].Size = 5;
coinArray[4].Size = 1;

...
wow shacktar, thanks! I had no idea that would cause it!

So I understand why that was happening, was it because my array "coinArray" was in main and my onHand() function was trying to compute values that it didn't have access to?
was it because my array "coinArray" was in main and my onHand() function was trying to compute values that it didn't have access to?

It comes down to the linker not being able to "see" coinArray from Coin.cpp. When compiling onHand, your extern Coin coinArray[5]; is basically telling the compiler that coinArray is defined someplace else and that it's up to the linker to find it later.

The reason that coinArray had to be in the global scope is so that its symbol would be exported. The way you had it was coinArray was a local variable. Local variables' symbols (i.e. names) aren't exported. Thus, the linker was not able to "see" coinArray.
Topic archived. No new replies allowed.