r/programminghelp • • 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?

4 Upvotes

7 comments sorted by

2

u/jedwardsol Nov 01 '20
if (number < min)

What is the value of number?

2

u/MaiganGleyr Nov 01 '20

The number is one of the numbers given.

As if, if AMOUNT is 5,m you give five NUMBERS, which can be any number between 0-9999.

2

u/jedwardsol Nov 01 '20

It's not - number is never initialised or assigned to. When you're reading in numbers you're overwriting amount

2

u/MaiganGleyr Nov 01 '20

So number is pretty much redundant?

2

u/jedwardsol Nov 01 '20

No - you don't want to overwrite amount.

You should read the numbers into number in the loop. Also in the loop you should use number to track the sum, min & max

2

u/[deleted] 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.