how to Cout the second min. number and max. number

How can i print out the SECOND MIN number and MAX number without using array?

please give me some advise.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
cout << "Please input a positive integer: ";
         cin >> input;
         min = input, max = input, secondmin = input;    
     do {
         cout << "Please input a positive integer: ";
         cin >> input;
         if (input == 0)       break;
         if (input > min && input > secondmin && input > max) {
            max = input;
         }
         if (input < secondmin) {
            if (input <= min) {
               secondmin = min;
               min = input;
            }
            else
                secondmin = input;
         }
         
     } while (input != 0);
        cout << "Second minimum: " << secondmin << endl << "Maximum: " << max << endl;
}
Try using \n instead of endl.
but this program can not run, some bugs occurred...
how to fixed it, or how to rebuild it?
Edit your post so we can see all the code, so we can build it... Or tell us the line where the error is...
how to rebuild it?

"Rebuild" implies that you have built it before. Do that again.

If the compiler finds an error, it will tell you. You should read those error messages.


As to the original question, there is a logical issue to decide:
What is the "secondmin" if input is:
1 5 1
Tell us what the error is.
Sorry for the late reply.

on the above coding, if i input the value like -> 1 2 3 4 5 6 7...

the expected result should be
Second min = 2, Maximum = 7.. but my result is Second min = 1, Maximum = 7... its wrong!!

however, if i input -> 7 6 5 4 3 2 1 .. i will get the right result.. how to change of my program? thanks
1. Tracking min is totally unrelated to tracking max.

2. What should the second* be, if input has only one value?

3. What should the secondmin be, if input is: 1, 5, 1?

4. When max changes, what do we know about the previous value of max?

5. When max does not change, what is the relation of the new value and secondmax?
Topic archived. No new replies allowed.