I want to make a program that asks to enter number.
The program will keep asking for entering number until the user enters -99.
And when the program finishes, it will give the number of counts, max, 2nd max, min, and 2nd min.

The program I made works for all except when I insert only two inputs containing one positive number and one negative number. (e.g. -12 and 89)

When I do that, it gives correct max, min, and min2, but 2nd max gives some random number.
How can I fix it?

-------------------------------------------------------------------------------------------

#include <iostream>
using namespace std;
int main()
{
    double number, max, min, max2, min2;
    int count;

    cout << "Enter nxt number (-99 to end): ";
    cin >> number;

    count = 0;

    max = number;
    min = number;


    while (number != -99)
    {
        cout << "Enter nxt number (-99 to end): ";
        cin >> number;
        count++;


        if (number > max)
        {
            max2 = max;
            max = number;
        }

        else if (number > max2)
        {
            max2 = number;
        }


        if (number == -99)
            break;

        if (number < min)
        {
            min2 = min;
            min = number;
        }

        else if (number < min2)
        {
            min2 = number;
        }
    }

    cout << "Count: " << count << "\n";
    cout << "Max: " << max << "\n";
    cout << "2nd Max: " << max2 << "\n";
    cout << "Min: " << min << "\n";
    cout << "2nd Min: " << min2 << "\n";

    return 0;
}

I have reviewed your above code.

first you need to initialize the min,min2 , max,max2 variables before use.

suggestion ,
initialize max,max2 to most minus value possible and
initialize min,min2 to most positive value possible in int.

something like that,

min = 0x7FFFFFFF;
 min2 = 0x7FFFFFFF;
 max = 0xFFFFFFFF;
 max2 = 0xFFFFFFFF;

and try your code again.

I want to make a program that asks to enter number.
The program will keep asking for entering number until the user enters -99.
And when the program finishes, it will give the number of counts, max, 2nd max, min, and 2nd min.

The program I made works for all except when I insert only two inputs containing one positive number and one negative number. (e.g. -12 and 89)

When I do that, it gives correct max, min, and min2, but 2nd max gives some random number.
How can I fix it?

-------------------------------------------------------------------------------------------

#include <iostream>
using namespace std;
int main()
{
    double number, max, min, max2, min2;
    int count;

    cout << "Enter nxt number (-99 to end): ";
    cin >> number;

    count = 0;

    max = number;
    min = number;


    while (number != -99)
    {
        cout << "Enter nxt number (-99 to end): ";
        cin >> number;
        count++;


        if (number > max)
        {
            max2 = max;
            max = number;
        }

        else if (number > max2)
        {
            max2 = number;
        }


        if (number == -99)
            break;

        if (number < min)
        {
            min2 = min;
            min = number;
        }

        else if (number < min2)
        {
            min2 = number;
        }
    }

    cout << "Count: " << count << "\n";
    cout << "Max: " << max << "\n";
    cout << "2nd Max: " << max2 << "\n";
    cout << "Min: " << min << "\n";
    cout << "2nd Min: " << min2 << "\n";

    return 0;
}

That's because you haven't initialized anything properly. You may want to consider initializing all values to the first input. Then, if you only have one input, all 4 values will output the same information (which makes sense because if you only have one value, that value naturally assumes all 4 roles).

cout << "Enter a value (-99 to exit): ";
cin >> inputValue;

while (-99 < inputValue) {
  if (count == 0) {
    min = min2 = max = max2 = inputValue;
    ++count;
  } else if ( ... ) {
    //...
  }

  //...

  cout << "Enter a value (-99 to exit): ";
  cin >> inputValue;

} //end while

Your requirements seem a little odd to me, but I'm sure there is a reason they are the way they are. This is my best interpretation of what they seem to be. This will stop the loop if the input is -99 or less (i.e. any negative value with an absolute value greater than or equal to 100 will also stop it).

I initialized as you have suggested, but now either the values of maximum and maximum2 or the values of minumum and minimum2 are the same. did i initialize wrong? ahh i just started learning c++, so this is killin me..:p

For a single input, they should all be the same.

After 2 input values, your results should be max == min2 and max2 == min.

After 3 inputs, min2 and max2 should match.

If after 4+ input, you still have some matching values, either your analysis is wrong or you aren't moving the values correctly. Most likely, your analysis is incorrect. The only exception to this would be if you entered the same value more than once (i.e. 25, 10, 30, 10 should produce min=10, min2=25, max2=25, max=30)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.