Right now I'm trying practice easy problems and working my way up to the hard ones, but I'm having a really hard time on this problem.
"Write a program that prompts the user to input 3 numbers. It should output the numbers in ascending order". I'm only getting two numbers but not the third number, please help.
#include <iostream>
usingnamespace std;
int main()
{
// declare int a,b,c
// use if and else if
int a,b,c;
cout << "Enter three numbers" <<endl;
cin >> a >> b >> c;
/* I was thinking to do this for my if statement below
if(a>b|| b<a || b>c || b<c)
cout << " " << a << " " << b << " " << c << endl;
not sure if that's what the program is asking for if I do it that way */
if(a > b || b<a)
cout << " " << a << " " << b << endl;
elseif (b>c || b<c)
cout << " " << b << " " << endl;
else
cout << "Wrong sequence " << endl;
return 0;
}
I am not seeing a c variable directed to output. I don't understand your code either. Anyway, here is a solution, if you're stuck (the foolproof method).
1 2 3 4 5 6 7 8 9 10 11 12 13 14
if (a > b && b > c)
std::cout << c << b << a;
elseif (b > c && c > a)
std::cout << a << c << b;
elseif (c > b && b > a)
std::cout << a << b << c;
elseif (b > a && a > c)
std::cout << c << a << b;
elseif (a > c && c > b)
std::cout << b << c << a;
elseif (c > a && a > b)
std::cout << b << a << c;
else
std::cout << "Wrong sequence";
Thank you, really do appreciated I figure out what I did wrong when looking at your code Drakonaut. And also thank you Arslan for letting me know to set the three numbers to equal each other. I'm glad you guys could help me out, I get frustrated and think I'm dumb because I can't solve these type of problems, but I know it will come over time just by practicing. Anyways here's the final code with your guys help:
#include <iostream>
usingnamespace std;
int main() {
// declare int a,b,c
// use if and else if
int a, b, c;
cout << "Enter three numbers" << endl;
cin >> a >> b >> c;
if (a >= b && b >= c)
cout << c << b << a;
elseif (b >= c && c >= a)
cout << a << " " << c << " "<< b << endl;
elseif (c >= b && b >= a)
cout << a << " " << b << " " << c << endl;
elseif (b >= a && a >= c)
cout << c <<" " << a << " " << b << endl;
elseif (a >= c && c >= b)
cout << b << " " << c << " "<< a << endl;
elseif (c >= a && a >= b)
cout << b << " " << a << " " << c << endl;
else
cout << "Wrong sequence";
return 0;
}