How to use one if inside the other?

Asked

Viewed 2,371 times

0

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

But I can not create three decisions, follows below my code.

#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;

}
  • Did any of the answers solve your problem? Do you think you can accept one of them? If you haven’t already, see [tour] how to proceed. You would help the community by identifying the best solution for you. You can accept only one of them, but you can vote for any question or answer you find useful on the entire site.

3 answers

5

You do not want to make one inside the other, although this would even work, but it is not ideal, the best is to make 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";
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • 1

    In the end an Else if would not be an if nested in Else?

  • @Guto, actually yes, the if resides within the else, but if you don’t usually use the keys in the else, in this case, for reasons of semantics, giving the impression of the else if be a unique structure. else if is with macros #define elif else if thus creates a unique structure. if(true){...}elif(!false){...}elif(a==b){...}

1

You must use the else if(statement)which means: "IF YOU DON’T PASS THE TOP IF, TRY THIS".

In your case it will stay:

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;

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;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.