Second maximum value in while loop

So the question is to input prices of books using while loop and find out the maximum price,second maximum price,minimum price and total number of books with maximum and minimum price. I've successfully done most part of the program but having one issues with the second max price thing.
lets say we input price as:
9
8
7
6
5
then the second max price is given equal to maximum price.
Please keep it simple like it is as its the requirement.

[code]
#include <iostream>
using namespace std;
int main()
{
int price=0,max=0,tmax=0,smax,tsmax=0,min=9999,tmin=0,i=0,buff=0;
cout<<"enter price"<<endl;
cin>>price;
smax=price;
while(price!=-1)
{

i=i+1;
if(max<price)
{
smax=max;
max=price;
tmax=0;
}
if(max==price)
tmax++;
if(min>price)
{
min=price;
tmin=0;
}
if(min==price)
tmin++;
cin>>price;
}
cout<< "Total No of books" << i << endl;
cout << "MAX PRICE = " << max << "~" << tmax << endl;
cout << "SECOND MAX PRICE = " << smax << endl;
cout << "MIN PRICE = " << min << "~" << tmin << endl;
system ("pause");
return 0;
}

Follow the values inside your variables while executing.
In the following example I’m trying to describe what happens to “smax” at the first execution of the scenario you required: 9 8 7 6 5 -1

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// So the question is to input prices of books using while loop and find out 
// the maximum price,second maximum price,minimum price and total number of 
// books with maximum and minimum price. I've successfully done most part of 
// the program but having one issues with the second max price thing.
// lets say we input price as:
// 9
// 8
// 7
// 6
// 5
// then the second max price is given equal to maximum price.
// Please keep it simple like it is as its the requirement.

#include <iostream>
using namespace std;
int main()
{
    int price=0,max=0,tmax=0,smax,tsmax=0,min=9999,tmin=0,i=0,buff=0;
    cout<<"enter price"<<endl;
    cin>>price; // let's enter 9 8 7 6 5 -1
    smax=price; // smax == 9; max == 0
    while(price!=-1)
    {
        i=i+1;
        if(max<price) // max == 0; price == 9 --> block executed
        {
            smax=max; // smax == 0 --> this will never change
            max=price;
            tmax=0;
        }
        if(max==price)
            tmax++;
        if(min>price)
        {
            min=price;
            tmin=0;
        }
        if(min==price)
        tmin++;
        cin>>price;
    }
    cout<< "Total No of books" << i << endl;
    cout << "MAX PRICE = " << max << "~" << tmax << endl;
    cout << "SECOND MAX PRICE = " << smax << endl;
    cout << "MIN PRICE = " << min << "~" << tmin << endl;
    system ("pause");
    return 0;
}

Yes -1 is like compulsory to end the loop but afterwards the output is still not right. Second max value is linked with maximum value and sometimes both value are similar.
Registered users can post here. Sign in or register to post.