Code works except for one part min max

#include <iostream>
using namespace std;

int main(){

int numbers; // Variable to store how many numbers will be entered
int num; // Variable to store the numbers entered
int max=0; // Variable max set to 0 so it can store the max of entered numbers
int min=0; // Variable min to store the smallest of the entered numbers
int sum; // Variable to sum up the entered numbers
float mean; // Varible that stores the mean of the entered numbers

cout << "How many numbers will be entered: ";// Prompt user to enter how many #s
cin >> numbers; // Store numbers in varible number

for(int i=0; i < numbers; i++){ // for loop that prompts user to enter numbers
cout << "Enter number: "; // until they reach there entered response
cin >> num;
if(num>max) // if statement that checks if number will be max
max=num; // if true stored in max
if(num<min) // if statemet that checks if number will be min
min=num; // if true stored in min
sum=sum+num; // sum up entered numbers
mean=sum/numbers; // takes the sum and divides by numbers entered
}

cout << "Max number is: " << max << endl; // Display min,max and mean
cout << "Min number is: " << min << endl;
cout << "Mean of numbers is: " << mean << endl;

}

it stores the max number and mean but m min is always 0. help
Commenting less than you should have is better than over-commenting.

Use code tags; otherwise the indentation is lost. http://www.cplusplus.com/articles/jEywvCM9/

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
#include <iostream>

int main()
{
    std::cout << "How many numbers will be entered? ";
    int cnt;
    std::cin >> cnt;

    int max = -999999999; // initialize with a value smaller than any that would be entered
    int min = +999999999; // initialize with a value larger than any that would be entered
    int sum = 0; // sum is initially zero

    for( int i=0; i < cnt; ++i )
    {
        std::cout << "Enter number (max six digits): ";
        int num ;
        std::cin >> num;

        if( num>max ) max = num;
        if( num<min ) min = num;
        sum = sum+num;
    }

    std::cout << "Max number is: " << max << '\n';
    std::cout << "Min number is: " << min << '\n';

    if( cnt > 0 )
    {
        const double mean = double(sum) / cnt; // we do not want integer division here
        std::cout << "Mean of numbers entered is: " << mean << '\n';
    }
}
Please use [code] [/code] tags -- it makes your code a lot easier to read.

You forgot to initialize your sum variable, and the line
mean=sum/numbers;
does integer division since sum and numbers are both int variables (so the result will always be an integer).
You can fix them by casting one or both of them to a floating-point number:
mean = static_cast<float>(sum) / numbers;

Other than that, your code works perfectly fine if you enter a negative number.
1
2
if(num<min) // if statemet that checks if number will be min
    min=num; // if true stored in min 

Since min starts out at 0, the only way num<min will be true is if num is a negative number.
Topic archived. No new replies allowed.