Hello, I've just begun programming, I'm trying to solve this problem but I think I've made a mess of it, could you please help, and explain where I've gone wrong
Problem /* ask the user to input 3 numbers, and then sort them from the lowest to the highest */
#include <stdlib.h>
#include <cstdio>
#include <iostream>
usingnamespace std;
int main ()
{
/* ask the user to input 3 numbers, and then sort them from the lowest to the highest */
int a,b,c,printa,printb,printc;
cout<<"1st number\n";
cin>>a;
cout<<"2nd number\n";
cin>>b;
cout<<"3rd number\n";
cin>>c;
for (int cont=0;cont<3;cont++) {
if (a < b && < c) {
printa=a;
cout<<"The first number is"<<printa;
elseif ( b < a && < c)
printa=b;
cout<<"The first number is"<<printa;
else
printa=c;
cout<<"The first number is"<<printa;
}
if (b > a && < c) {
printb=b;
cout<<"The second number is"printb;
elseif (a > b && < c)
printb=a;
cout<<"The second number is"printb;
else
printb=c;
cout<<"The second number is"<<printb;
}
if (a > c && > b) {
printc=a;
cout<<"The third number is"<<printc;
elseif (b > a && > c)
printc=a;
cout<<"The third number is"<<printc;
else
printc=c;
cout<<"The third number is"<<printc;
}
system("Pause");
return 0;
}
if (a > a && > b)
a can never be larger than a, this expression is allways FALSE
also: what does this loop accomplish?
You do the exactly same thing 3 times
1 2 3 4 5 6 7 8 9 10 11
for (int cont=0;cont<3;cont++) {
if (a < b && < c) {
printa=a;
cout<<"The first number is"<<printa;
elseif ( b < a && < c)
printa=b;
cout<<"The first number is"<<printa;
else
printa=c;
cout<<"The first number is"<<printa;
}
Well, that's not what's happening here :)
at the moment you just print 3 times "The first number is x" because it checks all if/elseif/else parts in each iteration
edit:
also: this is an invalid syntax && < c
you have to explicitly write the variable anew if ( a < b && a < c)