E. Colon
E. Colon

Reputation: 21

Why doesn't this code work correctly? (C++)

If I input this code:

#include<iostream>
using namespace std;
int main ()

{

int input, qty, min, max;
bool validity = 1;

cout << "How many integers would you like to enter? \n";
cin >> qty;

if (qty <= 0)
    cout << "Please enter a positive number\n";

else
{
    cout << "Please enter " << qty << " integers:" << endl;

    for (int x=0; x < qty; x++)
    {
        cin >> input;

        if (input > max)
            max = input;

        if (input < min)
            min = input;

    }

}


if (validity)
{
    cout << "Minimum: " << min << endl;
    cout << "Maximum: " << max << endl;
}


return 0;
}

It works as expected.

But if I have this:

#include<iostream>
using namespace std;
int main ()

{

int input, qty, min, max;
bool validity = 1;

cout << "How many integers would you like to enter? \n";
cin >> qty;

if (qty <= 0)
    cout << "Please enter a positive number\n";

else
{
    cout << "Please enter " << qty << " integers:" << endl;

    for (int x=0; x < qty; x++)
    {
        cin >> input;

        if (input > max)
            max = input;

        if (input < min)
            min = input;

    }

}

if (max > 2147483646)
{
    cout << "Please enter a valid value for integers." << endl;
    validity = 0;
}

if (min < -2147483647)
{
    cout << "Please enter a valid value for integers." << endl;
    validity = 0;
}

if (validity)
{
    cout << "Minimum: " << min << endl;
    cout << "Maximum: " << max << endl;
}


return 0;
}

It gives me erroneous values.

What am I doing wrong? Any help is greatly appreciated. (I'm a noob btw). Adding a little text here so that I can post this question.............................

Upvotes: 0

Views: 71

Answers (1)

TinyTheBrontosaurus
TinyTheBrontosaurus

Reputation: 4800

max and min are uninitialized. In C++, this means the values can be anything at all, unless in say Java where primitives are auto-initialized to 0.

One way to fix is to set a first flag, and set max and min the the first value entered.

bool first = true;
for (int x=0; x < qty; x++)
{
    cin >> input;

    if( first )
    {
        max = input;
        min = input;
        first = false;
    }
    if (input > max)
        max = input;

    if (input < min)
        min = input;

}

Here's what I mean by uninitialized. When starting up, min and max can be anything. Anything at all. Try it by printing out the value of min and max before your loop. It should (could) be different every time you run the program. Basically the value depends on what data was in that memory location the last time it was used.

So the if( input > max) is checking to see if input is greater than some random number between -2billion and 2 billion. (not useful). The first flag I put in there initializes min/max in the first iteration of the for loop, or the first value entered by the user, which is guaranteed to be both the min and the max of the entered value since it's the only entered value.

Upvotes: 2

Related Questions