Student Programmer needs help with functions

Last program of the semester. Need help writing writing this function.
the name of the function is getSales() it is passed by the name of the division.
* it asks the user for a divisions quarterly sales, validate input (cannot be less that $0.00), then returns it.

need help figuring this part out. the four divisions are northeast, southeast northwest, and southwest.

Would an array work here?
Last edited on
As I read it, the input to the function is a single name. The output is a single number. No need for any array, is there?
ok I misread it so I typed the wrong question.
double getSales is passed the name of the division
void findHighest is passed the four sales total.
sounds like findHighest could use an array perhaps.
I cannot figure out how to put all four divisions in the function. i am use to
 
double functionName( double num1, double num2)

How would i set up double getSales()?
do I put the names all four divisions in the parenthesis?
Last edited on
I feel like I would be guessing here. The information you gave so far has left me a bit confused as to what the requirements actually are for this project. However from what I think I've understood so far (and I could be wrong), the function should look like this: double getSales(string divName)
and the way in which you will handle the four divisions is to use a loop in the calling function, so that getSales() is called once for each division, but because it is in a loop it will be called four times in total.
Do ask for the sales input in main or the function?
This is what i have so far. I am having a mental block and have been since i started.
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
#include <iostream>
#include <iomanip>					// Needed for i/o manipulations.
#include <cstring>					// Needed to use single characters.
#include <string>					// Needed for strings of characters.

using namespace std;

// Function Prototype
double getSales(string divName);
void findHighest(double divSales);

int main()
{
// Character variable.
char again;
	
do
{

// there is code that needs to go here.



// Asks the user if they want to try again.
	cout << "Would you like to try again? (y or n)";
	cin >> again;
	 }while (again == 'Y' || again == 'y');
}

/****************************************************************
*			          Function : getSales                       *
* Is passed  the name of the division. Aks for user input and   *
* then returns it.                                              *
****************************************************************/

double getSales(string divName)
{
// for loop goes here?
}

/****************************************************************
*                    Function: void findHighest                 *
* Is passed the four sales totals and determines which is the   *
* highest  and prints the name and sales figure of the highest  *
* grossing division.                                            *
****************************************************************/

void findHighest(double divSales)
{
//not sure what to put in here.
}
This seems ok: double getSales(string divName)
Now you need to write the code to do this: "it asks the user for a divisions quarterly sales, validate input (cannot be less that $0.00), then returns it."

I don't think this is correct:
void findHighest(double divSales)

If the function receives a single parameter divSales it can't find the highest, as there's nothing with which to compare that value.

From the description: "Is passed the four sales totals and determines which is the highest and prints the name and sales figure of the highest grossing division." the function will need an array containing each of the sales figures. Because it needs to print the name of the division, it will need an array containing the names too.

So perhaps it might look something like this:
void findHighest(double divSales[], string divName[], int size)
Where the variable size specifies how many elements there are in the array.

But that's just one possibility, for example you might have a struct which contains both the name and sales figure, and pass a vector of those objects. There are many possible solutions.
so the code I am starting to put in the double getSales functions goes like this:
1
2
cout << "Northeast Division Sales: $";
cin >> neSales;

and so on until i have collected all 4.

I would have to validate each one seperately wouldn't I, because of the variable names being different?

And for the second function I know an array spots are counted 0 1 2 3 but as far as size would go I would still put 4 in, correc? I got a confused in class when we went over it because of other students talking.
Last edited on
But the function getSales() has a parameter divName,

so instead of this:
1
2
cout << "Northeast Division Sales: $";
cin >> neSales;

You would put this:
1
2
cout << divName << " Sales: $";
cin >> Sales;

or something of that style - general-purpose code which applies to any division.
i am getting this error message.'getSales' not all control paths return a value

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
75
76
77
78
79
80
81
82
83
84
85
using namespace std;

// Function Prototype
double getSales(string divName);
void findHighest(double neSale,double seSale,double nwSale,double swSale);

double neSales, seSales, nwSales, swSales, sales;
string name;

int main()
{
	// Character variable.
	char again;
	 

	do
	{
	cout << "Enter the sales for each division\n\n";
	cout << "NorthEast Division " << endl;
	neSales = getSales(divName);

	cout << "SouthEast Division " << endl;
	seSales = getSales(divName);

	cout << "NorthWest Division " << endl;
	nwSales = getSales(divName);

	cout << "SouthWest Division " << endl;
	swSales = getSales(divName);

findHighest(neSales, seSales, nwSales, swSales); 

// Asks the user if they want to try again.
	cout << "Would you like to try again? (y or n)";
	cin >> again;
	 }while (again == 'Y' || again == 'y');
}

/****************************************************************
*			          Function : getSales                       *
* Is passed  the name of the division. Aks for user input and   *
* then returns it.                                              *
****************************************************************/

double getSales(string divName)
{	double sales;

  
  //cout << divName << ": $";
  cin >> sales;
	
  
  if (sales < 0.00)
  {
	  cout <<cout << "You cannot have less than $0.00 in sales.\n";

		}
	else
   
return sales;

}

/****************************************************************
*                    Function: void findHighest                 *
* Is passed the four sales totals and determines which is the   *
* highest  and prints the name and sales figure of the highest  *
* grossing division.                                            *
****************************************************************/

void findHighest(double neSale,double seSale,double nwSale,double swSale)
{ 

if (neSales > seSales && seSales > nwSales && nwSales > swSales)
cout<<"divsion 1 has the highest gross"<<endl;

else if (seSales > neSales && neSales > nwSales && nwSales > swSales)
	cout <<"Division 2 has the highest gross." << endl;

else if (nwSales > neSales && neSales > seSales && seSales > swSales)
	cout << "Division 3 has the highest gross." << endl;

else
	cout << "Division 4 has the highest gross." << endl;
}
The compiler is being helpful here. It has spotted an error in the logic, where the function is supposed to return a value, but under certain circumstances, the function reaches the closing brace without having specified any value to be returned.

In the context here, a reasonable solution would be, if the user enters an unacceptable value, rather than exiting from the function, instead repeat the prompt message so the user knows what is expected, then get the input figure again. It might be necessary to do this more than once, so a while loop could be a good idea.

I don't want to distract too much by talking about too many different ideas at once. However at line 7 some global variables are declared,
 
double neSales, seSales, nwSales, swSales, sales;
and at line 74 onwards, those global variables are used within the function. But - that means that the function parameters double neSale,double seSale,double nwSale,double swSale are completely ignored. You should do one thing or the other. Either use global variables or pass parameters, but don't try to do both, it will only cause confusion.

My recommendation is to get rid of the global variables. Their use is often considered bad practice.
so i rewrote them as local variables both in main and in the void findHighest(double neSale,double seSale,double nwSale,double swSale) Because when I removed line 7 I was getting errors for not defining them, but I still get this error:
'getSales' : not all control paths return a value
and I also get these errors:
uninitialized local variable 'neSales' used
uninitialized local variable 'seSales' used
uninitialized local variable 'nwSales' used
uninitialized local variable 'swSales' used

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
75
76
77
78
79
80
81
82
83
using namespace std;

// Function Prototype
double getSales(string name);
void findHighest(double neSale,double seSale,double nwSale,double swSale);


string divName;

int main()
{
	// Character variable.
	char again;
	double neSales, seSales, nwSales, swSales;
	do
	{
	cout << "Enter the sales for each division\n\n";
	cout << "NorthEast Division " << endl;
	neSales = getSales(divName);

	cout << "SouthEast Division " << endl;
	seSales = getSales(divName);

	cout << "NorthWest Division " << endl;
	nwSales = getSales(divName);

	cout << "SouthWest Division " << endl;
	swSales = getSales(divName);

findHighest(neSales, seSales, nwSales, swSales); 

// Asks the user if they want to try again.
	cout << "Would you like to try again? (y or n)";
	cin >> again;
	 }while (again == 'Y' || again == 'y');
}

/****************************************************************
*			          Function : getSales                       *
* Is passed  the name of the division. Aks for user input and   *
* then returns it.                                              *
****************************************************************/

double getSales(string divName)
{
	double sales;

  
  //cout << divName << ": $";
  cin >> sales;
	
  
  if (sales < 0.00)
  {
	  cout <<cout << "You cannot have less than $0.00 in sales.\n";
		}
	else
   
return sales;

}

/****************************************************************
*                    Function: void findHighest                 *
* Is passed the four sales totals and determines which is the   *
* highest  and prints the name and sales figure of the highest  *
* grossing division.                                            *
****************************************************************/

void findHighest(double neSale,double seSale,double nwSale,double swSale)
{double neSales, seSales, nwSales, swSales;
if (neSales > seSales && seSales > nwSales && nwSales > swSales)  //All of the uninitialized local variable errors highlight this line
cout<<"Northeast Division has the highest gross with $"<< neSales << endl;

else if (seSales > neSales && neSales > nwSales && nwSales > swSales)
	cout <<"Southeast Division  has the highest gross. with $" << seSales << endl;

else if (nwSales > neSales && neSales > seSales && seSales > swSales)
	cout << "Northwest Division  has the highest gross with $" << nwSales <<  endl;

else
	cout << "Southwest Division has the highest gross with $" << swSales <<  endl;
}


and if I remove the local variables from the function i get these error plus 4 more.
Last edited on
You should definitely not be re-declaring the parameters as local variables inside the function. That just means that the parameters are still being ignored.

Instead, make sure that the names used in the function header match those used in the body of the function. In this case, the simplest answer is to change line 70 from
 
void findHighest(double neSale,double seSale,double nwSale,double swSale)
to
 
void findHighest(double neSales, double seSales, double nwSales, double swSales)

So i am guessing that it was a space's error after the comma, because it is fixed. Thank you so much!!!!!!!! You are a great help. Hopefully my mind freak will end after finals and I wont have so many problems next semester.
Last edited on
Not quite. The variable names are clearly different:
 
neSale
 
neSales
- that applies to all four of them, one ends in 's', the other doesn't.

Whitespace is ignored by the compiler and makes no difference.
Last edited on
Topic archived. No new replies allowed.