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?
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.
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.
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."
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
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.
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?
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!
#include <iostream>
usingnamespace 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: