### What's Wrong With my Min/Max Code Please Have a Look

I'm trying to write a program where the user will keep entering data until pressing control + D. Then the it will display the mean, smallest number, and largest number.

Here are three samples of my program running.

Sample 1
Enter the price for stock 1: \$ 10
Enter the price for stock 2: \$ 1.555
Enter the price for stock 3: \$ 3.648
Enter the price for stock 4: \$ 20
Enter the price for stock 5: \$
The average price is \$8.801
The max is \$20.000
The min is \$1.555

Sample 2
Enter the price for stock 1: \$ 15.000
Enter the price for stock 2: \$ 15.500
Enter the price for stock 3: \$ 15.999
Enter the price for stock 4: \$
The average price is \$15.500
The max is \$15.999
The min is \$15.500

Sample 3
Enter the price for stock 1: \$ 55.564
Enter the price for stock 2: \$ 213.222
Enter the price for stock 3: \$ 110.000
Enter the price for stock 4: \$
The average price is \$126.262
The max is \$213.222
The min is \$110.000

As you can see in Sample 1, the program runs correctly. The largest number was 20 and the lowest number was 1.555. But in Sample 2, the program shows min as 15.500, where it should be showing 15.000 and Sample 3, the program shows min as 110.000 when it should be showing 55.564.

Here's the code.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445`` ``````#include #include using namespace std; int main() { const int PRICE = -1; int stock = 1; float price, mean, min = PRICE, max = PRICE, total = 0; cout << "Enter the price for stock " << stock << ": \$ "; cin >> price; while (price > 0) { total += price; stock++; cout << "Enter the price for stock " << stock << ": \$ "; cin >> price; if (min == PRICE || price < min) min = price; if (max == PRICE || price > min) max = price; if (std::cin.eof()==1) break; } cout << fixed << showpoint << setprecision(3); mean = total / (stock-1); cout << "\nThe average price is \$" << mean << endl; cout << "The max is \$" << max << endl; cout << "The min is \$" << min << endl; return 0; } ``````

Any ideas on what is going on?

Maybe const int PRICE should be const float PRICE?
Just follow your programm flow from the beginning. You will see, that it completely ignores first value when it comes to min/max calculation. to counter this move lines 19 & 20 to the end of while loop.
kooth, thanks for the suggestion but it still has a problem finding min and max.

MiiNiPaa, I tried putting lines 19 & 20 at the end of the loop and even after the loop and it didn't fix the problem.

These are some great suggestions, does anyone have anymore?
line 25 the condition should be price > max
 ` I tried putting lines 19 & 20 at the end of the loop and even after the loop and it didn't fix the problem.`

P have tried that and it definetly fixes the problem. The example below. I have also fixed en error with max detection:
 ``12345678910111213141516`` `````` while (price > 0) { total += price; stock++; if (min == PRICE || price < min) min = price; if (max == PRICE || price > max) max = price; cout << "Enter the price for stock " << stock << ": \$ "; cin >> price; if (std::cin.eof()==1) break; }``````
Topic archived. No new replies allowed.