simple loop to compare numbers

Hi, i am working on a code, i am trying to compare the numbers from
Largest, Second Largest, Second Smallest and Smallest using a simple loop and use if and else statements to compare numbers as they are entered.

this is my code so far.
can anyone figure out what i need to write to make this code work?

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
#include <iostream>
#include <stdlib.h>
using namespace std;

int main()
{

int InputNum1;
int Smallest = 0;
int SecondSmallest = 0;
int Largest = 0;
int SecondLargest = 0;

cout << "Please enter a number between: 0 to 10000 any number less than zero program will not work" << endl;

cin >> InputNum1;


if (InputNum1 < 0 || InputNum1 > 10000)
    cout << " please try again " << endl;
else
    cout << "this works" << endl;

for (int loop=0;loop <20; loop++)

{cin >> InputNum1;
if (InputNum1 > Largest)
    {SecondLargest = Largest;
    Largest= InputNum1;}
else {   if  (InputNum1 > SecondLargest);
            {SecondLargest = InputNum1;}

if (InputNum1 > Smallest)
    { SecondSmallest = Smallest;
      Smallest = InputNum1;}
else{(InputNum1 < Smallest);
     Smallest = InputNum1;

}
}
cout << " largest  " << Largest << endl;
cout << " second largest  " << SecondLargest << endl;
cout << "second smallest  " << SecondSmallest <<endl ;
cout << " smallest  " << Smallest << endl;
}
return EXIT_SUCCESS;
}
Last edited on
closed account (48T7M4Gy)
Please use code tags.

What's not working? Please show some output.

You've missed some line too. There's no

1
2
return 0;
}


at the end, at least.
Last edited on
Hi kemort,

what do you mean by output? i am a noob sorry.

the code itself does work but it does not store numbers in the proper place.

i am using this code on (codeblocks)

closed account (48T7M4Gy)
Hi here are the instructions on using code block tags. They enable us to run your program.

If you are getting stuff printing out, 
just cut and paste it into your reply.

There are tags for the output that put it in a grey box like this sentence.



http://www.cplusplus.com/articles/jEywvCM9/
Last edited on
closed account (48T7M4Gy)
Also re-insert your program to include the missing lines I mentioned above.
hey man, im having the same problem as you, and it doesnt help that im in your class so we both cant help each other out, so hopefully we will be able to get answers from this site from some professional players.
Hey i hope this is better for you?

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
#include <iostream>
#include <stdlib.h>
using namespace std;

int main()
{

int InputNum1;
int Smallest = 0;
int SecondSmallest = 0;
int Largest = 0;
int SecondLargest = 0;

cout << "Please enter a number between: 0 to 10000 any number less than zero program will not work" << endl;

cin >> InputNum1;


if (InputNum1 < 0 || InputNum1 > 10000)
    cout << " please try again " << endl;
else
    cout << "this works" << endl;

for (int loop=0;loop <20; loop++)

{cin >> InputNum1;
if (InputNum1 > Largest)
    {SecondLargest = Largest;
    Largest= InputNum1;}

else {   if  (InputNum1 > SecondLargest);
            {SecondLargest = InputNum1;}

if (InputNum1 > Smallest)
    { SecondSmallest = Smallest;
      Smallest = InputNum1;}
else{Smallest = InputNum1;}

if (SecondLargest > SecondSmallest)

}
}
cout << " largest  " << Largest << endl;
cout << " second largest  " << SecondLargest << endl;
cout << "second smallest  " << SecondSmallest <<endl ;
cout << " smallest  " << Smallest << endl;
}
return EXIT_SUCCESS;
}


closed account (48T7M4Gy)
OK that's better. I'll have a look.
closed account (48T7M4Gy)
1
2
3
4
5
if (SecondLargest > SecondSmallest)

//} <---- WRONG ???
//} <---- WRONG ???
cout << " largest  " << Largest << endl;


And this is the output so far:

Please enter the password : 
Program terminated (signal: SIGKILL)
closed account (48T7M4Gy)
This is another brace in a possible wrong position

1
2
3
4
5
6
7
//} <---- WRONG ???
//} <---- WRONG ???
cout << " largest  " << Largest << endl;
cout << " second largest  " << SecondLargest << endl;
cout << "second smallest  " << SecondSmallest <<endl ;
cout << " smallest  " << Smallest << endl;
//} <---- WRONG ??? 


What you also need to do is properly indent your program listing so code parts are clear and the for loop 'range' is clear.

Also while you are in the debugging stage add to your password prompt what the correct password number is. And please make it a simple number. 123 will be OK for testing.
okay i am not sure what you mean by indenting the program listing?
i used the braces for the " if " statements so that whatever was in the 'main' brace is connected to the first "if statement."


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{cin >> InputNum1; 
if (InputNum1 > Largest)
    {SecondLargest = Largest;
    Largest= InputNum1;}

else {   if  (InputNum1 > SecondLargest);  <----- SECOND BRACE "ESLE"
            {SecondLargest = InputNum1;}

if (InputNum1 > Smallest)
    { SecondSmallest = Smallest;
      Smallest = InputNum1;}
else{Smallest = InputNum1;}

if (SecondLargest > SecondSmallest)

} <---- END OF THE ELSE "INSIDE" THE "IF"
} <----- END OF FIRST BRACE
cout << " largest  " << Largest << endl;
cout << " second largest  " << SecondLargest << endl;
cout << "second smallest  " << SecondSmallest <<endl ;
cout << " smallest  " << Smallest << endl;
//} <---- END OF MAIN BRACE FOR PROGRAM
closed account (48T7M4Gy)
https://en.wikipedia.org/wiki/Indent_style

Indentation is when you use tabs and layout so your program can be read clearly.

BTW you need to tag the code in the reverse of what you did above. You have put output tags around the code. The code goes in the purple box.

I know it is new to you and sounds fussy but it makes life easier for you and me.

I don't know what you mean about the braces. From my point of view you have a mystery problem you can't or won't describe and show the output you are getting. I have shown you the output I get when I make the changes I have shown you. If I didn't make the changes all I saw was a cursor blinking.

Unfortunately our mind readers are away today so you have to make it clear about you problems. If we can help we will but you need to try the changes suggested if you think they help and let us know on progress/output/what happened as a result.

Thanks.
Hey kemort i tried again with indenting the code properly. i cannot get the numbers to compare properly what do you think might be wrong with my logic?
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
#include <iostream>
#include <stdlib.h>
using namespace std;

int main()
{

int InputNum1;
int Smallest = 0;
int SecondSmallest = 0;
int Largest = 0;
int SecondLargest = 0;

cout << "Please enter a number between: 0 to 10000 any number less than zero program will not work" << endl;

cin >> InputNum1;


if (InputNum1 < 0 || InputNum1 > 10000)
    cout << " please try again " << endl;
else
    cout << "this works" << endl;

for (int loop=0;loop <20; loop++)

{
    cin >> InputNum1;
if  (InputNum1 > Largest)
{
    SecondLargest = Largest;
    Largest= InputNum1;
}
else
{
    SecondLargest = InputNum1;
}
if  (InputNum1 > SecondLargest)
{
    SecondSmallest = SecondLargest;
    SecondLargest = InputNum1;
}
else
{
    SecondSmallest = InputNum1;
}
if (InputNum1 > SecondSmallest)
{
    Smallest = SecondSmallest;
    SecondSmallest = InputNum1;
}
else
{
    Smallest = InputNum1;
}
if  (InputNum1 > Smallest)
{
    Smallest = InputNum1;
}
else
{
    InputNum1;
}
cout << " largest  " << Largest << endl;
cout << " second largest  " << SecondLargest << endl;
cout << "second smallest  " << SecondSmallest <<endl ;
cout << " smallest  " << Smallest << endl;
}
return 0;
}
closed account (48T7M4Gy)
Wow, that's an improvement - I'll have a look.
closed account (48T7M4Gy)
I'm just look at you code but I have a question already. Why not store the numbers in an array and then sort them? Haven't you done array yet?
This code is for a project. it limits my ability to only use basic if and else statements to make the comparison of numbers between 0 and 10,000 using 2 max and 2 min outputs in a loop where the outputs change as the user enters a new number.
but yes an array would be ideal , that is my next upcoming project!
closed account (48T7M4Gy)
Here is a code fragment of the sort of thing you need to do

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main()
{

    int InputNum1;

    int Smallest = 0;
    int SecondSmallest = 1;
    int Largest = 2;
    int SecondLargest = 3;

    cin >> InputNum1;

    for (int loop = 0; loop < 5; loop++)
    {
        // Prompt required
        cin >> InputNum1;
    
        if  ( InputNum1 > Largest )
            Largest= InputNum1;

        if  ( InputNum1 > SecondLargest and InputNum1 < Largest )
            SecondLargest = InputNum1;

etc etc blah blah blah
closed account (48T7M4Gy)
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
#include <iostream>

using namespace std;

int main()
{
    int a = 0, b = 0, c = 0, d = 0;
    int n1 = 0, n2 = 0, n3 = 0, n4 = 0;
    
    cout << " Please enter a number: ";
    cin >> a;
   
    cout << " Please enter a number: ";
    cin >> b;
    if ( b < a ){ n1 = b; n4 = a;}
    else { n1 = a; n4 = b; }
    
    cout << " Please enter a number: ";
    cin >> c;
    if ( c <= n1 ) { n3 = n2 = n1; n1 = c; }
    if ( c >= n4 ) { n3 = n2 = n4; n4 = c; }
    if ( c > n1 && c < n4) { n3 = n2 = c; }
    
    for (int i = 0; i < 20; i++ )
    {
        cout << " Please enter a number: ";
        cin >> d;
        
        if ( d <= n1 ) { n2 = n1; n1 = d; }
        if ( d > n4 ) { n3 = n4; n4 = d; }
        if ( d > n1 && d <= n2 ) { n2 = d; }
        if ( d > n2 && d <= n3 ) { /* just for completeness */}
        if ( d > n3 && d < n4 ) { n3 = d; }
        
        std::cout << n1 << ' ' << n2 << ' ' << n3 << ' ' << n4 << std::endl;
    }
    return 0;
}




Please enter a number: 600
 Please enter a number: 876
 Please enter a number: 4
 Please enter a number: 89
4 89 600 876
 Please enter a number: 123
4 89 600 876
 Please enter a number: 900
4 89 876 900
 Please enter a number: 2
2 4 876 900
 Please enter a number:  
Last edited on
If you check out your original code, you had > instead of < for the smallest/second smallest comparisons.
Topic archived. No new replies allowed.