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;
}