I am stuck there is a problem in my code but I do not see where it is.
The user is suppose to enter three numbers and then they are to be displayed in numerical order.
This should be simple but I screwed it up. This is homework so I am looking for help, not someone to do my work.
Code:
#include <iostream>
using namespace std;
int main()
{
double num1, num2, num3, first, second, third; // variable declaration
num1 = num2 = num3 = first = second = third = 0;
cout << "Please Enter Three Numbers \n";
cin >> num1 >> num2 >> num3;
first = num1;
if (num2 <= first)
first = num2;
third = num1;
if (num3 <= first)
first = num3;
if (num1 >= first)
if (num1 <= third)
second = num1;
if (num2 >= first)
if (num2 <= third)
second = num2;
first = num3;
third = num1;
if (num3 >= first)
if (num3 <= third)
second = num3;
cout << first << ", " << second << ", " << third << ".";
Thank you both, I didn't mean to come across rude if I did. I was under the assumption from my instructor that if we left out {} that it would not compile. I tried you advice and it seems to have only made my problem worse.
Thank you both, I didn't mean to come across rude if I did. I was under the assumption from my instructor that if we left out {} that it would not compile. I tried you advice and it seems to have only made my problem worse.
Post the new code inside code tags (i.e [ CODE] [/ CODE] minus the spaces).
Unfortunately until we know exactly what you did, and what your program did, and what you don't like about that, it's hard to suggest anything further.
Here is my updated code that made it worse. The program is supposed to input three numbers from the user and then display them back in numeric order. After I compile it and run it it will duplicate numbers or give me zeros instead of the numbers input.
Code:
#include <iostream>
using namespace std;
int main()
{
double num1, num2, num3, first, second, third; // variable declaration
num1 = num2 = num3 = first = second = third = 0;
cout << "Please Enter Three Numbers \n";
cin >> num1 >> num2 >> num3;
first = num1;
if (num2 <= first)
{
first = num2;
third = num1;
}
if (num3 <= first)
{
first = num3;
}
if (num1 >= first)
if (num1 <= third)
{
second = num1;
}
if (num2 >= first)
if (num2 <= third)
{
second = num2;
first = num3;
}
if (num3 >= first)
if (num3 <= third)
{
second = num3;
}
cout << first << ", " << second << ", " << third << ".";
Quite a complicated scheme.....which is why real programs wouldn't sort that way. (Imagine trying to write out that sort of code if you had to sort 1 million items rather than 3.) Still, I suppose it's informative as a starting point.
Your approach isn't terribly principled even so, though. You may want to rethink it a bit.
I presume when you have two if statements in a row, you're trying to express a logical "AND". You can just use:
Code:
if (num1 >= first && num1 <= third)
As for why you're getting particular values out: Step into the code with your debugger and see why they are what they are.
if (num1 >= first)
if (num1 <= third)
{
second = num1;
}
if (num2 >= first)
if (num2 <= third)
{
second = num2;
first = num3;
}
Code:
if (num1 >= first)
{
if (num1 <= third)
{
second = num1;
}
if (num2 >= first)
{
if (num2 <= third)
{
second = num2;
first = num3;
}
}
}
or
Code:
if (num1 >= first)
{
if (num1 <= third)
{
second = num1;
}
}
if (num2 >= first)
{
if (num2 <= third)
{
second = num2;
first = num3;
}
}
And there are several other possibilities. Btw, the whole reason for this exercise is to have you understand the difference between how the code executes differently when an if statement is enclosed within curly braces.
Now, I am completely confused. I greatly thank you for the help. But I can't figure it out and now you guys have made me completely unsure of what I wanted! lol
If you had two numbers, there would only be two cases:
A B
B A
When you introduce C, each of the two cases above can be expanded 3 ways:
C A B
A C B
A B C
C B A
B C A
B A C
for a total of six possible orderings. Even if you wrote out the conditions required for each explicitly, eg (C <= B && B <= A) for C B A, it wouldn't be substantially more code than you have now.
Bookmarks