### How to fix this error?

I have wrote the following program:

 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 #include #include #include using namespace std; const int DIVISIONS = 8; double getSales(string); void findHighest(int [], int); // Function: getSales // Purpose: To get a division's quarterly sales and // return it to main // Inputs: double quarterlySales: Stores division's // quarterly sales // Returns: The quarterly sales of a division double getSales(string division) { double quarterlySales; cout << "What is the " << division << " division's quarterly sales? \$"; cin >> quarterlySales; cin.ignore(); if (quarterlySales <= 0) { cout << "You must enter a positive number!\n\n"; return 0; } return quarterlySales; } // Function: findHighest // Purpose: To find the division with the highest sales // Inputs: string divisionName: Stores the division's name // double largestNumber: Stores the winning division's // sales figure // double i: Loop counter // Returns: The winning division's name and sales figure void findHighest(int division_array[], int size) { int largestNumber; for (int i = 0; i < size; i++) { if (division_array[i] > largestNumber) { largestNumber = i; } } cout << "The sales figure for the winning division is: \$" << fixed << setprecision(2) << largestNumber << endl; } int main() { double divisionSales[DIVISIONS]; string NORTHWEST = "NORTHWEST", NORTHEAST = "NORTHEAST", SOUTHWEST = "SOUTHWEST", SOUTHEAST = "SOUTHEAST"; cout << "The four divisions:\n\nNORTHWEST\nNORTHEAST\nSOUTHWEST\nSOUTHEAST\n\n"; divisionSales[0] = getSales(NORTHWEST); divisionSales[1] = getSales(NORTHEAST); divisionSales[2] = getSales(SOUTHWEST); divisionSales[3] = getSales(SOUTHEAST); findHighest(divisionSales[DIVISIONS], DIVISIONS); cin.get(); return 0; }

I'm getting an error that says I'm trying to turn something into a pointer. We haven't covered pointers in my class yet, so I don't know whats going on in my program.

The program is suppose to make use of arrays to gather how much money each region made and then output the winner and the sales figure.

Am I passing the arrays to the functions correctly here?
Your intuition was correct, you aren't passing the array to findHighest properly.
 12345 //change this: findHighest(divisionSales[DIVISIONS], DIVISIONS); //to this: findHighest(divisionSales, DIVISIONS);

Originally, you were passing divisionSales[DIVISIONS] to your function, which is divisionSales[8], which is one value. You want to pass the whole array, so you use the arrays name as the parameter.
What exactly is the error? What line does it appear on?
Another thing is that you should set largesNumber = 0 whenever you declare it. If you don't it's value will be so really high number (not sure exactly what but none of the values in division_array will be greater than it so it will cause a problem.
Only thing I can see really is on line 70 you pass the array incorrectly to the findHighest function. You just pass the name of the array and either it's size as an int, or 2 pointers pointing to the first element and the address following the last element.
Thank you, changing that let me compile the program.

When I type in the amount of sales each division gets, it always says the winning sales is \$7, regardless of whats typed in. Do you see what could be causing this error? I thought I had the "find the largest number" algorithm correct.
Last edited on
The reason you're always getting 7 is because in the for loop in findHighest(), you're setting largestNumber to i, when you should set it to division_array[i]. i will always equal size-1 when the for loop has ended, so largest number will always equal size-1.

Another thing you need to address is that you're passing an array of doubles as an array of ints. divisonSales[] is a double, while your function, findHighest(int array[], size) calls for an int. This could cause extreme rounding errors (or a compiler error).

And finally:
 123456 divisionSales[0] = getSales(NORTHWEST); divisionSales[1] = getSales(NORTHEAST); divisionSales[2] = getSales(SOUTHWEST); divisionSales[3] = getSales(SOUTHEAST); findHighest(divisionSales[DIVISIONS], DIVISIONS); //DIVISIONS is not the size of the array at the moment.

You've only defined four members in divisionSales, not the max size. By passing DIVISIONS to that function, you're iterating over members of divisionSales that haven't been initialized (they inherit whatever random value is in the memory slot they're allocated.) It's possible that you could get some of these random, uninitialized values that are larger than anything you input, causing your function to yield incorrect results.
To fix this, you should pass the amount of used slots in divisionSales as the size parameter, not it's maximum amount of slots.
Last edited on
Topic archived. No new replies allowed.