Having trouble with this program

closed account (yR9wb7Xj)
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.
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
#include <iostream>
using namespace 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;

	else if (b>c || b<c)
		cout << " " << b << " " << endl;
	else
		cout << "Wrong sequence " << endl;




	return 0;
}
Last edited on
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;
	else if (b > c && c > a)
		std::cout << a << c << b;
	else if (c > b && b > a)
		std::cout << a << b << c;
	else if (b > a && a > c)
	    std::cout << c << a << b;
	else if (a > c && c > b)
	    std::cout << b << c << a;
	else if (c > a && a > b)
	    std::cout << b << a << c;
	else
	    std::cout << "Wrong sequence";


Using if and else if.
You should also include code for if the three numbers entered are equal or if two out of the three are equal.
closed account (yR9wb7Xj)
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:
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
#include <iostream>
using namespace 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;
	else if (b >= c && c >= a)
		cout << a << " " << c  << " "<< b << endl;
	else if (c >= b && b >= a)
		cout << a << " "  << b << " " << c << endl;
	else if (b >= a && a >= c)
		cout << c <<" " << a << " " << b << endl;
	else if (a >= c && c >= b)
		cout << b << " " << c  << " "<< a << endl;
	else if (c >= a && a >= b)
		cout << b << " " << a << " " << c << endl;
	else
		cout << "Wrong sequence";
		
	return 0;
}
Last edited on
Topic archived. No new replies allowed.