Ok, so I've been trying to some practice programs and have come to a slight problem.

Here's the program I need to work on: Write a program that determines which of a company's four divisions (NE, SE, NW, SW) had the greatest sales for a quarter. It should include the following two functions, which are called by main.

*double getSales() is passed the name of a division. It askes the user for a division's quarterly sales figure, validates the input, then returns it. It should be called once for each division.

*void findHighest() is passed the four sales totals. It determines which is the largest and prints the name of the high grossing division, along with it's sales figure.

Here's the first code I written:

#include<iostream>
#include<iomanip>
using namespace std;

double getSales();

int main()
{

double sales;

	cout << "Please enter sales for NE" << endl;
	sales = getSales();
	cout << "Please enter sales for SE" << endl;
	sales = getSales();
	cout << "Please enter sales for NW" << endl;
	sales = getSales();
	cout << "Please enter sales for SW" << endl;
	sales = getSales();
	return 0;
}

double getSales()
{
	double Sales;
	cin >> Sales;

	return Sales;
}

Now I haven't enterted the void function yet. But is it ok if I able to use if statements in the void function?, but that might confuse it, so what is the best way to get around this problem?

I also have written it using arrays, but i don't know how to call arrays through functions. Here is the code with arrays:

#include<iostream>
#include<iomanip>
using namespace std;




int main()
{
	char name[4][12] = {"NE Division", "SE Division", "NW Division", "SW Division"};
	double S[4];
	double highest;
	int high; 
	
	cout << "Please enter the quarterly sales for each division" << endl;
	for (int count = 0; count < 4; count++)
	{
		cout << "Please enter the sales for " << name[count] << endl;
		cin >> S[count];
	}

	highest = S[4];
	for (int count = 0; count < 4; count++)
	{
		if (S[count] > highest)
		{
			high = count;
			highest = S[count];
		}
	}
	cout << "The division with the highest sales is " << name[high] << " with a total of $" << highest << endl;
	return 0;
}

Thanks for any future help. I apprciate it so much.
I still have a ways to go in terms of C++.

>>Now I haven't enterted the void function yet. But is it ok if I able to use if statements in the void function?,

All a void means is that the function will not return anything. Other than that, there is no restrictions on what void functions can do or the type of statements is can contain. If you think your void function needs if statements then by all means include them.

>> highest = S[4];
That should be hightest = S[0]; . There is no 4 in array S, only 0, 1, 2 and 3.

Edited 7 Years Ago by Ancient Dragon: n/a

line 22 second program has out of bounds index used for S. If S has 4 elements, then the largest valid index is 3, as index values are zero based, meaning the first valid index is zero.

S probably could be a 2 dimensional array, like this: double S[4][4]; rather than a unidimensional array so it can hold all 4 quarters worth of sales data for each of the four divisions of the company. Alternatively you will need 4 one dimensional arrays (in parallel), one for each divisions sales data. Either approach could work. As a last alternative you might be able to do this with a single array, or even no array at all if you use a variable for maxSales and compare it with each new set of sales input. However, if you want to do more than just calculate and compare total sales, then you will need one of the first two methods described above to keep track of the details and not just the largest sales total.

Edited 7 Years Ago by Lerner: n/a

*double getSales() is passed the name of a division. It askes the user for a division's quarterly sales figure, validates the input, then returns it. It should be called once for each division.

*void findHighest() is passed the four sales totals. It determines which is the largest and prints the name of the high grossing division, along with it's sales figure.

See red. Read the spec carefully. Both of these functions need to be passed parameters. Currently they do not take parameters. Change them so that they do. I'm not sure this is a very good spec. The findHighest function needs to be able to print "the name of the highest grossing division". However, it is not passed any names, so it doesn't have enough information to do that part of the job. Perhaps you are supposed to also supposed to pass the function the names?

Where did you get this spec? I would change the void function so it is also passed the names. Using an array is fine. I can't imagine why it wouldn't be OK to use an if statement inside of the functions. I don't see why that would "confuse" anything. Normally you would use at least one if statement when you calculated the maximum. You could possibly do it without using one, but there's absolutely no reason why you would want to, so go ahead and use an if statement. You don't "call" an array "through" a function, you "pass" an array "to" a function. See the example code at the bottom of this link.

http://www.cplusplus.com/doc/tutorial/arrays/

Edited 7 Years Ago by VernonDozier: n/a

This article has been dead for over six months. Start a new discussion instead.