Problem returning a value from a function.

I am having problems returning the value that I want from my function getPrices(). This part of the program is supposed to prompt the user to take up to 25 prices or exit with a -1. Every time I display the count though, all I get is zero. Any help on this would be greatly appreciated.

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
#include <iostream>
using namespace std;

// Function Prototypes
int getPrices(double price[], int array_size);
void calcStats(double price[], int array_size);
 

int main()
{
	const int array_size = 25;
	double price[array_size] = {};
	int count = 0;
	int num;

	

	// Prompt user to enter up to 25 prices or a -1 when done
	cout << "Enter up to 25 prices or a -1 when done.\n";
	
	// Call to function getPrices()
	getPrices(price, array_size);

	
	cout << endl;
	cout << "The number of prices entered was: ";
	cout << count << endl;

	
	return 0;
}	


//***********************************************************************
//                       Function getPrices()                           *
//                  Gets input (prices)from user                        *
//***********************************************************************

int getPrices(double price[], int array_size)
{

	double input = 0.0;
	int num = 0;
	int count = 0;

	while (input != -1 && count < array_size)
	{
	
		cout << "Enter price " << (count + 1) << " \n";
		cin >> input;
		price[count] = input;
		count++;
		
		if (input == -1)
		{
			price[count - 1] = NULL;
			return count;
		}
	}
	return count;
}


Last edited on
closed account (LA48b7Xj)
Write

 
count = getPrices(price, array_size);


instead of

 
getPrices(price, array_size);
Awesome! Thank you.
Topic archived. No new replies allowed.