need to return more than one value?

Pages: 12
This function is in a separate .cpp source file from main.cpp. I'd like to have this function output the 5 values of change[5]. Is this possible?

For testing purposes, I have it returning change[4] at the moment and it works, but how might I get this to return each position in the array to 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
30
31
32
33
34
35
36
37
38
39
40
41
42

int giveBack(double dispense)
{
	int change[5] = {0, 0, 0, 0, 0};
	
	while(dispense > 0)
	{
		if(dispense >= 100)
		{
			change[0]++;
			dispense -= 100;
			continue;
		}
		if(dispense >= 25)
		{
			change[1]++;
			dispense -= 25;
			continue;
		}
		if(dispense >= 10)
		{
			change[2]++;
			dispense -= 10;
			continue;
		}
		if(dispense >= 5)
		{
			change[3]++;
			dispense -= 5;
			continue;
		}
		if(dispense >= 1)
		{
			change[4]++;
			dispense -= 1;
			continue;
		}
		
	}
	
	return change[4];
}
Or, is it possible to make my array "change[5]" in this other source file, visible to main.cpp so I can

cout << change[0] << change[1] << ... etc. ?
Last edited on
The array change[5] is defined as a local object within the function. What you could do instead is to define the array in function main() and pass the array as a parameter to your function giveBack().
Last edited on
Thanks Chervil, that makes sense. I'm getting an error when compiling and its due to line 3. Am I using this wrong? Can I pass two variables to this function?

1
2
3
4
5
6
7
8
if(dispense / 100 < onHand())
	{
		cout << "Your change is " << giveBack(dispense, change[5]) << endl;
	}
	else
	{
		cout << "Not enough money in machine!" << endl;
	}
so maybe you need to return a pointer to the first element in the array?
try something like this

1
2
3
4
5
6
int * giveBack(double dispense)
{
int *change = new int[5];
// assign values however you want
return change;
}


then, in main, call back like this:
1
2
3
4
5
6
7
8
int * p_returned;
double pass_dispense; // I dunno what u r using to pass double to giveBack
cout << "dispense: "; cin >> pass_dispense; cout << '\n';
p_returned = giveBack(pass_dispense);
for (int x=0; x<5; x++)
{
    cout << p_returned[x] << '\n';
}


or, alternatively, if you declare the array globally, as chervil suggests ^, giveBack can be "void giveBack(double dispense);"
and all will be good.. and that may be better for your situation

btw I am not at home right atm to compile the example I made above so if I'm doing something wrong, someone please kindly point that out
Last edited on
Yes I declared my array globally, but when I pass that array to my giveBack() function, along with the double dispense that I was passing before, I get an error. The error is long but I can post it if it would help?
well, I don't think you actually need to "pass the array" as it were, if it's declared globally. If you want to use that strategy just declare the array in main, and it can be accessed by any other function.. and then you can make giveBack like this:

1
2
void giveBack(double dispense);
void giveBack(double dispense){}


This way there's no need to actually "return" anything from the giveBack. It will use the global array "change[]" which can be accessed from"int main(){}" or "void giveBack(double){}"

In other words, the code stays same as original post, just move declaration of change[5] to "main()" and get rid of the "return change[4]" at the end. that line is bad anyway.

I should also say you'd do yourself good to learn how to return a pointer from a function, but I don't want to press the issue too much until I know the code I wrote in the above post is solid. x3
If anyone can run it through a thing real quick to verify, that'd make me feel better
Last edited on
I thought having all of my code might make my problem easier to see.

My issue is when I compile, I'm getting the error:
1
2
3
4
5
Undefined symbols for architecture x86_64:
  "giveBack(double, int)", referenced from:
      _main in ccNNnGiV.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status


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

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

extern Coin coinArray[5];
extern double onHand();
extern int change[5];
extern void giveBack(double, int);
extern double dispense;

#endif 


coin.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
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

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

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

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;

}

void giveBack(double dispense, int change[5])
{
	
	while(dispense > 0)
	{
		if(dispense >= 100)
		{
			change[0]++;
			dispense -= 100;
			continue;
		}
		if(dispense >= 25)
		{
			change[1]++;
			dispense -= 25;
			continue;
		}
		if(dispense >= 10)
		{
			change[2]++;
			dispense -= 10;
			continue;
		}
		if(dispense >= 5)
		{
			change[3]++;
			dispense -= 5;
			continue;
		}
		if(dispense >= 1)
		{
			change[4]++;
			dispense -= 1;
			continue;
		}
		
	}
	
	return;
}


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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include "coin.h"

using namespace std;

Coin coinArray[5];
int change[5] = {0, 0, 0, 0, 0};
double dispense = 0;

int main()
{

coinArray[0].Size = 100;
coinArray[1].Size = 25;
coinArray[2].Size = 10;
coinArray[3].Size = 5;
coinArray[4].Size = 1;
	
	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 << "\nThat makes $" << onHand() << " in machine.\n" << endl;
	
	cout << "How much money (in pennies) would you like dispensed?" << endl;
	cin >> dispense;
	
	
	if(dispense / 100 < onHand())
	{
		giveBack(dispense, change[5]);
		cout << "Your change is:" << endl;
		cout << change[0] << " dollars." << endl;
		cout << change[1] << " quarters." << endl;
		cout << change[2] << " dimes." << endl;
		cout << change[3] << " nickels." << endl;
		cout << change[4] << " pennies." << endl;
	}
	else
	{
		cout << "Not enough money in machine!" << endl;
	}

	return 0;
}


The line giving the error is in main.cpp line 33
Last edited on
just edited my last post ^ u read
I think you're getting an error for trying to pass an array to a function.
Once again, using either method you choose to handle "change" I've outlined above, you do not need to pass it to the function, nor return it from a function.
It can be useful in this situation however, and indispensable in others, to return a POINTER from a function.
Last edited on
Line 33 should read giveBack(dispense, change);
That way, you pass the array (or in effect a pointer to the array).


The existing (incorrect) code passes just a single element of the array, which is an ordinary integer.
Last edited on
honestly the only thing you need to change from your last posted code:

take the ", change[5]" out of the definition and callback of "giveBack"

and get rid of the "return" at the end of "giveBack"
..because its called "void giveBack()"

and it should work as is.. see what happens

p.s. you are avoiding using pointers at the expense of making a global array..
it will work, but there's another more elegant way.
Last edited on
Chervil, Thanks, that did get rid of my previous error, now it's reporting:
1
2
3
main.cpp: In function ‘int main()’:
main.cpp:33: error: invalid conversion from ‘int*’ to ‘int’
main.cpp:33: error:   initializing argument 2 of ‘void giveBack(double, int)’


I'm not sure what line 2 means, change[5] is just an int array
The declaration @line 16 of coin.h doesn't match the definition @line 24 of coin.cpp.

Line 33, when you do giveBack(dispense, change[5]);, you are passing the function the 6th element in the array, not the array itself.

When passing arrays through functions, it is typical to pass the size of the array as well. I don't think you need any size specifier in the function dec/def:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int total(int arr[], const int SIZE);

int main(void)
{
  const int MSIZE = 5;
  int mArr[MSIZE] = {1,2,3,4,5};
  
  std::cout << total(mArr, MSIZE) << '\n';
  return 0;
} 

int total(int arr[], const int SIZE)
{
  int t = 0;
  for (int i = 0; i < SIZE; i++)
    t += arr[i];
  return t;
}
I'm not sure if I could possibly answer your problem any more thoroughly.

Everywhere you have:

"giveBack(dispense, change[5])"

it needs to say

"giveBack(dispense)"

and make the variable you are passing TO the function (dispense) have a different name WITHIN the function.. not sure if that causes an error but it makes me shudder.

Once again, you have declared "change[5]" globally.
There is no need to pass it to the function.
And if you wanted to make it local instead, you need to use new
and return a POINTER from the function to the new array. But lets just get the code you have now to work and worry about that later.

Anyway make sure you get that function definition and callback straightened out, then post any error msgs you get after that's fixed
Last edited on
cPlusN00b, I'm sorry, I must have overlooked something in your previous post, you are absolutely right. I quit passing my array to that function and it worked! Thank you.
You're quite welcome! :3

Remember for future reference:

Don't pass an array to, or return an array from, a function.
Instead, pass or return a pointer to the first element in an array from the function.
You will be soooo happy times when you see the light
Last edited on
I had a rather lacking 'c language' class, now I'm struggling a bit in c++... I need to learn pointers, sad eh?
Not at all eh, pointers can be a real headache at first.

btw if you wanna start playing around with them, my best advice is:
don't be confused by the two different meanings of *

it is used to declare a pointer:
int * a_pointer;

and also to "de-reference" a pointer:
1
2
3
int a_number;
a_pointer = &a_number;
*a_pointer = 4;


the * in the second example means like "variable pointed to by"
so in this example the last line assigns a value of 4 to the integer pointed to by a_pointer.

more complete code:
1
2
3
4
5
6
int a_number;          // declare an integer
int * a_pointer;       // declare a pointer to an integer
a_pointer = &a_number; // assign a (reference to a_number) to a_pointer
*a_pointer = 4;        // assign value of 4 to integer pointed to by a_pointer (which is a_number)
cout << *a_pointer;    // print integer pointed to by a_pointer
cout << a_number;      // see? it's the same kinda 

I put those bolds there to translate the * and & in each line..
notice how the * in the second line means something totally different than the * in all the subsequent lines..
in the second usage, you can see how '*' is like opposite of '&' in a way..

I hope that helps to make more sense and not confuse lol sometimes I'm not the most concise
Last edited on
I guess this comes down to the design philosophy.
There are two arrays,
1
2
Coin coinArray[5];
int change[5];

which are declared globally in main.cpp

Since they are global, there is no need to pass them as parameters. However the same may also be said of the variable double dispense; which is also declared at the global scope. So it too does not need to be passed as a variable.

There are arguments for and against the use of global variables, but I think its fair to say its generally not regarded as the best practice.

Often there are beginner programs I see which have everything declared with global scope, and the outcome is chaotic. At best it makes the program harder to read and follow, at worst it leads to unexpected side-effects.

At the very least, regardless of how you choose to proceed, you should at least be aware of and know how to pass an array as a parameter, should the need arise.
agree w Chervil

if you'd be so kind look at this and make sure I'm doing it right
would this be the good way to keep everything local?

1
2
3
4
5
6
int * giveBack(double dispense)
{
int *change = new int[5];
// assign values however you want
return change;
}


main:
1
2
3
4
5
6
7
8
9
10
11
12
int * p_returned;
double pass_dispense;

cout << "dispense: ";
cin >> pass_dispense;
cout << '\n';

p_returned = giveBack(pass_dispense);
for (int x=0; x<5; x++)
{
    cout << p_returned[x] << '\n';
}


or did I declare p_returned wrong? should it be:
"int p_returned[5]" ?
is it sinful to use "new" without "delete"?

I'm not around a compiler right now so can't check my work :/
Last edited on
Pages: 12