How to use an if within the other?

0

The problem is the following 2 soccer teams, wanted to create a decision where it would show "Brazil won" or "Argentina Won" or "There was a tie."

But I can not make three decisions, here is my code below.

#include<iostream>
using namespace std;

int main(){
    int a, b;

    cout<<"Digite quantos Gol(s) o Brasil fez: ";
    cin>>b;
    cout<<"Digite quantos Gol(s) a Argentina fez: ";
    cin>>a;

    if (b>a){
        cout<<"\nBrasil ganhou com "<<b<<"Gol(s)\n";
        /*if (b==a){
            cout<<"Brasil e Argentina empatou, ambos com "<<a<<"Gol(s)";
        }*/

    }
    else (a==b)
        cout<<"Houve empate. "<<a<<"Gol(s)";

    else 
        cout<<"\nArgetina ganhou com "<<a<<"Gol(s)\n";

    return 0;

}
    
asked by anonymous 27.06.2016 / 18:23

3 answers

5

You do not want to do one in the other, although this would work, but it is not ideal, it is best to do them in sequence:

int main() {
    int a, b;
    cout << "Digite quantos Gol(s) o Brasil fez: ";
    cin >> b;
    cout << "Digite quantos Gol(s) a Argentina fez: ";
    cin >> a;
    if (b > a) {
        cout << "\nBrasil ganhou com "<< b << "Gol(s)\n";
    } else if (a == b) {
        cout << "Houve empate. " << a << "Gol(s)";
    } else  {
        cout << "\nArgetina ganhou com " << a << "Gol(s)\n";
    }
}

See running on ideone and on CodingGround .

    
27.06.2016 / 18:28
1

You should use else if(statement) which means: "IF YOU DO NOT PASS IN THE UPPER IF, TRY THIS."

In your case it will be:

cout<<"Digite quantos Gol(s) o Brasil fez: ";
cin>>b;
cout<<"Digite quantos Gol(s) a Argentina fez: ";
cin>>a;

if (b>a){
    cout<<"\nBrasil ganhou com "<<b<<"Gol(s)\n";
    /*if (b==a){
        cout<<"Brasil e Argentina empatou, ambos com "<<a<<"Gol(s)";
    }*/

}
else if(a==b){
    cout<<"Houve empate. "<<a<<"Gol(s)";

} else {
    cout<<"\nArgetina ganhou com "<<a<<"Gol(s)\n";
}

return 0;
    
27.06.2016 / 18:28
0
cout<<"Digite quantos Gol(s) o Brasil fez: ";
cin>>b;
cout<<"Digite quantos Gol(s) a Argentina fez: ";
cin>>a;

if (b>a){
    cout<<"\nBrasil ganhou com "<<b<<"Gol(s)\n";
}else{ 
    if (b==a){
        cout<<"Brasil e Argentina empatou, ambos com "<<a<<"Gol(s)";
    }else{
       cout<<"\nArgetina ganhou com "<<a<<"Gol(s)\n";
   }
}

return 0;
    
27.06.2016 / 18:28