0

hi ive developed a code to sort 3 numbers and pick out the middle one. the numbers are distinct.

#include<iostream>
using namespace std;
int main( ) {
int X, Y, Z;
cout<<"please 1st number ";
cin>>X;
cout<<"please 2nd number ";
cin>>Y;
cout<<"please 3rd number ";
cin>>Z;
if (Z>X>Y) (Y>X>Z)
cout<<"this is the middle number "<<X<<endl;
else if (X>Y>Z) (Z>Y>X)
cout<<"this is the middle number "<<Y<<endl;
else if (X>Z>Y) (Y>Z>X)
cout<<"this is the middle number "<<Y<<endl;
else
cout<<"either there are two numbers of equal value or you entered an invalid input";
system("pause");
return 0;
}


i tested it with using just
if (Z>X>Y) (Y>X>Z)
cout<<"this is the middle number "<<X<<endl;

however once ive addded the 'else if' statements the program said somethign was wrong and would not compile. i tested the else if with
#include<iostream>
using namespace std;
int main( ) {
char code;
cout << "Enter a marital code:\n";
cin >> code;
if (code =='M')
cout << " .. married\n"<<endl;
else if (code == 'S')
cout << " .. single\n"<<endl;
else if (code == 'D')
cout << " .. divorced"<<endl;
else
cout << " .. invalid coded entered.\n";
return 0 ;
}

and it worked perfectly. so what am i doing wrong with the first code?

any help would be great thanks.
-Leone

5
Contributors
9
Replies
10
Views
7 Years
Discussion Span
Last Post by xLeonex
0

Please use code tags when you post code. Also, please include the compiler output. "It said something is wrong" is pretty hard for us to debug!

0
#include<iostream>
using namespace std;
int main( ) {
int X, Y, Z;
cout<< "please 1st number  ";
cin>>X;
cout<<"please 2nd number  ";
cin>>Y;
cout<<"please 3rd number  ";
cin>>Z;
if (Z>X>Y)(Y>X>Z); 
cout<<"this is the middle number "<<X<<endl;
else if(X>Y>Z)(Z>Y>X);
cout<<"this is the middle number "<<Y<<endl;
else if(X>Z>Y)(Y>Z>X);
cout<<"this is the middle number "<<Y<<endl;
else
cout<<"either there are two numbers of equal value or you entered an invalid input";
system("pause");
return 0;
}

sorry and
this is what the messages were
13 .cpp expected primary-expression before "else"
13 .cpp expected `;' before "else"

continued for line 15 and 17

-1

Your conditional formatting is not correct.

You can not string multiple relational statements together in the standard algebraic format (i.e. x>y>z). You must create individual relations and connect them using either a logical AND '&&' or a logical OR '||'.

//use of logical AND
if (x>y && y>z)

//use of logical OR
if (x>y || y>z)

I suggest you read up on your operators.

Edited by Fbody: n/a

0

ah yes the AND/OR thnx ill fix my code.

#include<iostream>
using namespace std;
int main( ) {
int X, Y, Z;
cout<<"please 1st number  ";
cin>>X;
cout<<"please 2nd number  ";
cin>>Y;
cout<<"please 3rd number  ";
cin>>Z;
if (Z>X && Z>Y)(Y>X && Y>Z);
cout<<"this is the middle number "<<X<<endl;
else if(X>Y && X>Z)(Z>Y && Z>X);
cout<<"this is the middle number "<<Y<<endl;
else if(X>Z && X>Y)(Y>Z && Y>X);
cout<<"this is the middle number "<<Y<<endl;
else
cout<<"either there are two numbers of equal value or you entered an invalid input";
system("pause");
return 0;
}

unforunately the same error message pops up
ive even tested it with

if(Z>X && Z>Y)(Y>X && Y>Z);
cout<<"this is the middle number "<<X<<
else if(X>Y && X>Z);
cout<<"this is the middle number "<<Y<<endl;
else if(Z>Y && Z>X);
cout<<"this is the middle number "<<Y<<endl;

and no sucess i think its something to do with my use of 'else if' but i can't seem to pin point it.

0

You can't put a semicolon at the end if the "if" line. Google should know about plenty of tutorials to get you started.

0

You need to use logicals to combine the various compound statements as well:

if((Z>X && Z>Y)&&(Y>X && Y>Z))
  cout<<"this is the middle number "<<X;

Although, i suspect you can do it more intuitively if you vary your use of the operators.
(i.e. combine '<' and '>' and '&&' and '||')

Edited by Fbody: n/a

0

i think i can help but plz first clear me the Question. u said u want to SORT and CHOOSE middle one. do u want to do:

take input first, then sort them in ascending order and then select middle number from what means the sorted ones or the values stored in variables before?

why not u use an array? it will also be good approach.

0

Well, You cannot put the semicolon at the end of if statement and hope it to see the else if clause statement correctly.

if (x > y );                        // remove this semi-colon.
printf ( " x is greater than y. " );
else 
printf ( " y is greater than x. " );

Just omit semicolon at the end of if statement.
Just check if-else, or if -else if are compound statements, that is else won't work without if statement.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.