else without a previous if with an if in the code.

#include<iostream>
using namespace std;

int main(){

float numero,numero2,numero3, resultado=0;

cout<< "write the value of the numbers: "; cin>> numero>> numero2 >> numero3;

if(numero>numero2<numero3){
cout<< "the first number is greater";
}

if else(numero<numero2>numero3){
cout<< "the second number is greater";
}
else{
cout<< "the third number is greater";
}

return 0;
}
if else(numero<numero2>numero3){

should be

else if (numero>numero2<numero3){

That said, this: numero>numero2<numero3 makes no sense at all. You can only compare two items at a time.

Perhaps you mean:
1
2
 if(numero  > numero2  &&
    numero2 < numero3){


Similar in other places.
Last edited on
closed account (SECMoG1T)
hello mastagraft,
1
2
3
4
5
///if(numero>numero2<numero3) is wrong
if(numero>numero2 && numero2<numero3)//should be like this

///if else   is wrong
else if /// should be like this. 
As already mentioned, the relational operators are binary operators (take two operands) and they return a bool.
We could use functions rather than operators:
bool AlessthanB( float A, float B ); // returns same as A < B

Now, lets write x < y < z with that function:
1
2
3
4
float x, y, z;
if ( AlessthanB( AlessthanB( x, y ), z ) ) // error
{
}

Lets do that in steps:
1
2
3
4
bool XY = AlessthanB( x, y );
if ( AlessthanB( XY, z ) ) // error
{
}

The XY is a bool, but the AlessthanB() requires float.

Sadly, the x < y < z is not an error; there are implicit conversions, but the compiler should warn about it.

There is a way to write the tests without the logical AND (&&); nested ifs:
IF A<B
THEN // A is not the greatest
    IF C<B
    THEN B
    ELSE C
ELSE // B is not the greatest
    IF C<A
    THEN A
    ELSE C
Humans are smarter than computers. It cannot understand (A < x < B)
But it does understand (x > A && x < B).
Your logic is also faulty.
(numero>numero2<numero3) checks for if both "numero" and "numero3" is greater than "numero 2", not if "numero" is greatest, if that's what you're aiming for.
Using "else" for the last statement is wrong because you don't know if that else statement means "if numero3 is the greastest", "else" rather means EVERYTHING ELSE.

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
34
35
36
37
38
#include <iostream>

using namespace std ;

int main()
{

    float numero1, numero2, numero3, resultado = 0 ;

    cout << "Write the value of 3 numbers: " << endl ; 
    cin >> numero1 ;
    cin >> numero2 ;
    cin >> numero3 ; 

    if( numero1 > numero2 && numero1 > numero3) { 
        cout << "The first number is greatest." << endl ;
    }
 
    else if( numero2 > numero1 && numero2 > numero3 ) {
        cout << "The second number is greatest." << endl ;
    }
    
    else if( numero3 > numero1 && numero3 > numero2 ) {
        cout<< "The third number is greatest." << endl ;
    }
    
    else {
        cout << "Either the numbers are the same, cannot be compared or invalid inputs. Please exit and try again." << endl;
        return 0 ;
    }
    
    //sample code using "resultado" to follow proper coding format for beginners
    resultado = ( numero1 + numero2 + numero3 ) ;
    cout << "The sum is " << resultado ;
    
    return 0 ;
    
}


Never be lazy when coding C++. Follow my format the next time and you'll be fine.
Last edited on
Topic archived. No new replies allowed.