Loop storing 2 max numbers and 2 min numbers.

Hi there i am having trouble with my program, what the logic is implying is that every time i enter a big number, it should save as the biggest integer, if i enter a smaller number than the biggest, it will save in my second biggest integer slow. this works, but what i don't get how to do is do the same with the storing the low and the lowest numbers . it will save the low and lowest, but if i enter a number higher than what is already low, it will change that value when it shouldn't. i know how it should work i just can't apply that logic in the program. help please!
p.s. everything works, all brackets work, fully functioning code

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <cstdlib>
using namespace std;

int main()


{
    //give names to all of your integers
    int highest = 0;
    int high = 0;
    int low = 0;
    int lowest = 0;
    int number_from_KB;

//telling the user to enter a number between 0 and 10000 between specifications
        cout<<"enter a number between 0 and 10000"<<endl<<endl;
        cout<<"If your number falls to -1, the program will end"<<endl<<endl;



//beginning of the loop
    for (int loop=0;loop<15;loop++)

   {

       cout<<" Enter a valued number "<<endl;

        cin>>number_from_KB;

        if (number_from_KB==-1)
            break;

        if (number_from_KB<0 || number_from_KB>10000)  
            cout<<"This is wrong. Do it again "<<endl;  


        else                                           
        {
                cout<<"Good choice. "<<endl<<endl;


                if (number_from_KB>=highest)       
                {
                      high=highest;
                      highest=number_from_KB;
                                                
                }
                else                             
                {
                    if (number_from_KB>=high)
                    {
                        high=number_from_KB;
                    }
                    else                        
                    {
                        if (number_from_KB>=low)
                        {
                        lowest=low;
                        low=number_from_KB;
                        }

                        else                  
                        {
                            if (number_from_KB>=lowest && number_from_KB<=low)
                            {
                                low=number_from_KB;
                            }
                            else
                            {
                                if (number_from_KB>=0)
                                {
                                    lowest=number_from_KB;
                                }
                            }

                        }
                    }
                }

        cout<<"Your numbers are "<<highest<<" and "<<high<<" and "<<low<<" and "<<lowest<<endl<<endl;
        cout<<"The highest number that you entered was: "<<highest<<endl;
        cout<<"The second highest number that you entered was: "<<high<<endl;
        cout<<"The second lowest number that you entered was: "<<low<<endl;
        cout<<"The lowest number that you entered was: "<<lowest<<endl;
        }
   }
}
Last edited on
bump
When using ‘if’ and ‘else’ commands while writing your code it is important to remember to end the statement properly using semicolons. Upon adding lines in the code it is necessary to add {braces} around the extra lines of code you input, so the ‘if’ or ‘else’ command will go through all of your extra code at once. If these lines are not in a brace under the ‘if’ or ‘else’ statement the program will not know where this code goes in the loop.

Start but typing the first part which would be:
If (condition = TRUE) then you may add something for it to do if the condition is met correctly
{Something to do ;} make sure to end the line using ‘;’ then add the else prompt
Else (condition=FALSE) then add something to do
{Something to do ;} the command will execute as long as this info rejects the first if statement end with a semicolon so the line finishes
Topic archived. No new replies allowed.