r/programminghelp • u/MaiganGleyr • Nov 01 '20
C++ What is the problem with MIN-line
So hi, it's me again, the wannabe-beginner coder idiot. Greetings!
So off to another problem I am having.
In this "program" if you will, you give an X amount of numbers. The "program" then sums them up, prints the sum, prints the mean of those numbers, and then it should print the biggest and smallest of the given numbers. As you can see in the code, I guess.
Except, it doesn't.
It gives me only the biggest, which is defined as MAX. As MIN, it also gives me the result of MAX.
#include <iostream>
#include <conio.h>
using namespace std;
int main(void)
{
//variables
int counter = 0, amount, number, sum = 0, mean = 0, min = 999, max = 0;
cout << "How many numbers do you want to input? ";
cin >> amount;
while (counter < amount)
{
cout << "Give me a number" << counter + 1 << " ";
cin >> amount;
sum = sum + number;
mean = sum / amount;
counter = counter + 1;
}
if (amount < 0 || (number + 1) < 0 || sum < 0 || mean < 0)
{
cout << "Well it's big fat fucking ERROR!";
}
else
{
cout << "Sum of the given numbers is " << sum;
cout << "\nMean of the given numbers is " << mean;
}
if (number < min)
{
min = number;
cout << "\nSmallest number is " << min;
}
if (number > max)
{
max = number;
cout << "\nBiggest number is" << max;
}
_getch();
return 0;
}So, I ask you, the wise of the r/programminghelp:
Where's the poop?
2
Nov 01 '20
Normally one would be checking the min and max inside the while, so that it can be adjusted on the entry of -each- number. You're doing it all at the end which means it will only be based on whatever value 'number' has in it. Bad form.
2
u/MaiganGleyr Nov 01 '20
And whaddaya know, it worked.
I thank you many times, good sir. Firm handshakes.
2
u/jedwardsol Nov 01 '20
What is the value of
number?