Help, compile error using string inside a switch in c ++

0

I greet you, I just started to program and I find this problem "[Error] 'binary1' was not declared in this scope" is a string inside a switch, a string seemed outside the switch works for me, but not inside , if you can help me. The code is not finished alone, it's just that I get stuck in this part.

#include <iostream>
#include <string>

using namespace std;

int main (){

int opcion;
/* string para menu*/
string menu1,menu2,menu3,menu4,salir;
menu1 = "1. MENU BINARIO";
menu2 = "2. MENU DECIMAL";
menu3 = "3. MENU HEXADECIMAL";
menu4 = "4. MENU OCTAL";    
salir = "5. SALIR";
cout<<menu1<<endl<<menu2<<endl<<menu3<<endl<<menu4<<endl<<salir<<endl;
/*bucle dowhile que solo elija una opcion valida*/
do {
cout<<"Elija un menu :";cin>>opcion;
} while (opcion<1 || opcion>5);
/*switch general de menu*/  
switch (opcion){
    case 1 : {// estas llaves las puse por que me tiraba otro error si no las ponia.
             int opcion2;
             std::string binario1,binario2,binario3,salir2;// lo mismo con el std:: delante del string y cout.
             binario1 = "1. Pasar de binario a decimal";
             binario2 = "2. Pasar de binario a hexadecimal";
             binario3 = "3. Pasar de binario a octal";
             salir2 = "4. salir";}
             std::cout<<"MENU BINARIO"<<endl<<binario1<<endl<<binario2<<endl<<binario3<<endl<<salir2<<endl;
             /*bucle dowhile que solo elija una opcion valida*/
             do{
             cout<<"Digite una opcion :";cin>>opcion2;
                }while (opcion2<1 || opcion2>4);
             switch (opcion2){
                case 1 :
                case 2 :
                case 3 :
                case 4 :
                defualt : break;
             }break;
    case 2 : int opcion3;
             {
             std::string decimal1,decimal2,decimal3,salir3; 
             decimal1 = "1. Pasar de decimal a binario";
             decimal2 = "2. Pasar de decimal a hexadecimal";
             decimal3 = "3. Pasar de decimal a octal";
             salir3 = "4. Salir";}
             std::cout<<"MENU DECIMAL"<<endl<<decimal1<<endl<<decimal2<<endl<<decimal3<<endl<<salir3<<endl;
             /*bucle dowhile interno para que solo elija una opcion*/
             do{
             cout<<"Digite una opcion :";cin>>opcion3;
                }while (opcion3<1 || opcion3>4);
                /*Switch interno case 2*/
                switch (opcion3){
                    case 1 :
                    case 2 :
                    case 3 :
                    case 4 :
                    defualt :;
                }break;
    case 3 : int opcion4;
             { // estas llaves las puse por que me tiraba otro error si no las ponia.
             std::string hexa1,hexa2,hexa3,salir4; 
             hexa1 = "1. Pasar de hexadecimal a binario";
             hexa2 = "2. Pasar de hexadecimal a decimal";
             hexa3 = "3. Pasar de hexadecimal a octal";
             salir4 = "4. Salir";}
             std::cout<<"MENU HEXADECIMAL"<<endl<<hexa1<<endl<<hexa2<<endl<<hexa3<<endl<<salir4<<endl;
             /*bucle dowhile interno para que solo elija una opcion*/
             do{
             cout<<"Digite una opcion :";cin>>opcion4;
                }while (opcion4<1 || opcion4>4);
                /*Switch interno case 3*/
                switch (opcion4){
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    default:;
                }break;
    case 4 :int opcion5; 
            {
             std::string octal1,octal2,octal3,salir5;
             octal1 = "1. Pasar de octal a binario";
             octal2 = "2. Pasar de octal a decimal";
             octal3 = "3. Pasar de octal a hexadecimal";
             salir5 = "4. Salir";}
             std::cout<<"MENU OCTAL"<<endl<<octal1<<endl<<octal2<<endl<<octal3<<endl<<salir5<<endl;
             /*bucle dowhile interno para que solo elija una opcion*/
             do{
                cout<<"Digite una opcion :";cin>>opcion5;
             }while (opcion5<1 || opcion5>4);
             /*Switch interno case 3*/
             switch (opcion5){
                case 1 :
                case 2 :
                case 3 :
                case 4 :
                default :;
             }break;
    case 5 : return (exit);
    default :;
}

return 0;
system ("pause");
}             
    
asked by Sham del Prado 18.05.2016 в 10:11
source

2 answers

1

Please correct me if I'm looking bad:)

  • Your code is very messy, you must learn a bit to format (indented), make strange calls and in your switch structures you call defualt no a default .

  • Within case 2 and case 4 you declare a variable outside the keys, when the compiler waits for a break instruction, it has a } out of place to the right of the% assignment co_de % as well as the salir3 , case 1 and case 3 .

  • You're already calling case 4 so you do not have to use using namespace std; in each definition.

  • If you use std:: remember to also use std::cout , but as I mentioned in point 3, you're already calling std::endl .

  • You can not call namespace std after the instruction system("pause"); when you reach return 0; and the function ends and therefore the execution of the program also (since the function return ).

  •  The code fixed (it worked for me, I changed main for system("pause") because I'm in linux):
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main () {
    
        int opcion;
    
        string menu1, menu2, menu3, menu4, salir;
    
        menu1 = "1. MENU BINARIO";
        menu2 = "2. MENU DECIMAL";
        menu3 = "3. MENU HEXADECIMAL";
        menu4 = "4. MENU OCTAL";    
        salir = "5. SALIR";
    
        cout << menu1 << endl << menu2 << endl << menu3 << endl << menu4 << endl << salir << endl;
    
        do {
            cout << "Elija un menu :"; cin >> opcion;
        } while (opcion < 1 || opcion > 5);
    
        // Main switch
        switch (opcion) 
        {
            case 1 : 
            {
                int opcion2;
                string binario1, binario2, binario3, salir2;
    
                binario1 = "1. Pasar de binario a decimal";
                binario2 = "2. Pasar de binario a hexadecimal";
                binario3 = "3. Pasar de binario a octal";
                salir2 = "4. salir";
    
                cout << "MENU BINARIO" << endl << binario1 << endl << binario2 << endl << binario3 << endl << salir2 << endl;
    
                do {
                    cout<<"Digite una opcion :"; cin >> opcion2;
                } while (opcion2 < 1 || opcion2 > 4);
    
                switch (opcion2) {
                    case 1 :
                    case 2 :
                    case 3 :
                    case 4 :
                    default : break;
                } 
            } break;
    
            case 2 : 
            {
                int opcion3;
                string decimal1, decimal2, decimal3, salir3;
    
                decimal1 = "1. Pasar de decimal a binario";
                decimal2 = "2. Pasar de decimal a hexadecimal";
                decimal3 = "3. Pasar de decimal a octal";
                salir3 = "4. Salir";
    
                cout << "MENU DECIMAL" << endl << decimal1 << endl << decimal2 << endl << decimal3 << endl << salir3 << endl;
    
                do {
                    cout << "Digite una opcion :"; cin >> opcion3;
                } while (opcion3 < 1 || opcion3 > 4);
    
                switch (opcion3) {
                    case 1 :
                    case 2 :
                    case 3 :
                    case 4 :
                    default : break;
                }
            } break;
    
            case 3 : 
            { 
                int opcion4;
                string hexa1, hexa2, hexa3, salir4; 
    
                hexa1 = "1. Pasar de hexadecimal a binario";
                hexa2 = "2. Pasar de hexadecimal a decimal";
                hexa3 = "3. Pasar de hexadecimal a octal";
                salir4 = "4. Salir";
    
                cout<< "MENU HEXADECIMAL" << endl << hexa1 << endl << hexa2 << endl << hexa3 << endl << salir4 << endl;
    
                do {
                    cout<< "Digite una opcion :"; cin >> opcion4;
                } while (opcion4 < 1 || opcion4 > 4);
    
                switch (opcion4) {
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    default: break;
                }
            } break;
    
            case 4 :
            {
                int opcion5; 
                string octal1, octal2, octal3, salir5;
    
                octal1 = "1. Pasar de octal a binario";
                octal2 = "2. Pasar de octal a decimal";
                octal3 = "3. Pasar de octal a hexadecimal";
                salir5 = "4. Salir";
    
                cout<<"MENU OCTAL"<< endl << octal1 << endl << octal2 << endl << octal3 << endl << salir5 << endl;
    
    
                do {
                    cout<< "Digite una opcion :"; cin >> opcion5;
                } while (opcion5 < 1 || opcion5 > 4);
    
                switch (opcion5) {
                    case 1 :
                    case 2 :
                    case 3 :
                    case 4 :
                    default : break;
                } 
            } break;
    
            case 5 : return 0;
            default : break;
        }
    
        cin.get();
        return 0;
    
    }      
    

    As mentioned in the eferion response, you must have good writing practices, since most of the errors are syntax errors, the most common are cin.get() errors at the end of the sentence and that kind of thing.

    Try to have a better control over what you write by indentating the code and giving it a special format.

    ~ Greetings!

        
    answered by 18.05.2016 / 14:34
    source
    3

    Variables have a context that marks their life cycle. When the code leaves the context of a variable, that variable ceases to exist:

    int main()
    {
      int a = 0; // El ámbito de esta variable es la función main
    
      if( a==0 )
      {
        int b = 1; // El ámbito de b es el if
      }
    
      a = 4; // OK porque seguimos dentro del ámbito de a
      b = 5; // ERROR: b no existe porque hemos abandonado su ámbito
    }
    

    The flow control structures ( if , if-else , switch ) do not create scopes, but it is necessary to create them explicitly and that is why behind a if we usually find an opening key:

    if( algo )
    { // Inicio del ámbito
      // ...
    }  // Fin del ámbito
    

    Of course, the if can go without keys, but in that case the instruction will only affect the immediately following instruction:

    if( algo )
      printf("Hola"); // Dentro del if
    printf(" Mundo"); // Fuera del if
    

    The case of switch is slightly different. To understand each other we could assume that switch is a kind of alias that translates into a kind of jumps with goto .

    An example of a switch:

    int valor;
    // ...
    switch( valor )
    {
      case 0:
       // ...
       break;
    
      case 1:
       // ...
    
      case 2:
      case 3:
        break;
    
      default:
    }
    
    valor = 5;
    

    A possible translation:

    int valor;
    
    // ...
    
    if(valor == 0)
      goto CASE0;
    else if(valor == 1)
      goto CASE1;
    else if(valor == 2)
      goto CASE2;
    else if(valor == 3)
      goto CASE 3;
    else
      goto FIN;
    
    CASE0:
    // ...
    goto FIN;
    
    CASE1:
    // ...
    
    CASE 2:
    CASE 3:
    goto FIN;
    
    FIN:
    valor = 5;
    

    That is, the code that we put in each case is not enclosed by default within a scope but the program separates (without being too strict) the different sections with jumps. This behavior explains why you can not declare variables within switch without using braces:

    int main()
    {
      int valor;
      switch( valor )
      {
        case 0:
          int a;
    
        case 1:
      }
    }
    
    int main()
    {
      int valor;
      if(valor == 0)
        goto CASE0;
      else if(valor==1)
        goto CASE1;
      else
        goto FIN;
    
      CASE0:
      int a;
    
      CASE1:
      // Técnicamente estamos dentro del ámbito de a
      // Sin embargo la misma puede no haber sido declarada.
    
      FIN:
      // Aquí seguimos dentro del ámbito de a y tenemos el mismo problema
    }
    

    However, when you enter the keys the thing changes:

    int main()
    {
      int valor;
      switch( valor )
      {
        case 0:
        {
          int a;
        }
        case 1:
      }
    }
    
    int main()
    {
      int valor;
      if(valor == 0)
        goto CASE0;
      else if(valor==1)
        goto CASE1;
      else
        goto FIN;
    
      CASE0:
      {
        int a;
      }
    
      CASE1:
      // Ya no estamos en el ámbito de a, luego estamos seguros
      // de que aquí no existe
    
      FIN:
      // Ya no estamos en el ámbito de a, luego estamos seguros
      // de que aquí no existe
    }
    

    With this explanation I hope that the concept of the scope of the variables is a little clearer.

    Now, if we pass to your code we see the following (well tabulated):

    case 1:
    {// Inicio de ámbito.
      int opcion2;
      std::string binario1,binario2,binario3,salir2;// lo mismo con el std:: delante del string y cout.
      binario1 = "1. Pasar de binario a decimal";
      binario2 = "2. Pasar de binario a hexadecimal";
      binario3 = "3. Pasar de binario a octal";
      salir2 = "4. salir";
    } // Fin de ámbito, las variables declaradas aquí dentro dejan de existir
    
    // Esta instrucción ya no es válida porque binario1 y compañía
    // no existen
    std::cout<<"MENU BINARIO"<<endl<<binario1<<endl<<binario2<<endl
                                   <<binario3<<endl<<salir2<<endl;
    /*bucle dowhile que solo elija una opcion valida*/
    do{
      cout<<"Digite una opcion :";cin>>opcion2;
    }while (opcion2<1 || opcion2>4);
    
    switch (opcion2){
      case 1 :
      case 2 :
      case 3 :
      case 4 :
      default: break;
    }
    break;
    

    The origin of the problem is clearly seen (which is commented on in the code for clarity). When leaving the area where the variables have been declared, they have ceased to exist and, therefore, can not be used.

    The problem is solved by expanding the scope everything we need:

    case 1:
    {// Inicio de ámbito.
      int opcion2;
      std::string binario1,binario2,binario3,salir2;// lo mismo con el std:: delante del string y cout.
      binario1 = "1. Pasar de binario a decimal";
      binario2 = "2. Pasar de binario a hexadecimal";
      binario3 = "3. Pasar de binario a octal";
      salir2 = "4. salir";
    
      // Como no hemos salido del ámbito, las variables pueden ser usadas.
      std::cout<<"MENU BINARIO"<<endl<<binario1<<endl<<binario2<<endl
                                   <<binario3<<endl<<salir2<<endl;
      /*bucle dowhile que solo elija una opcion valida*/
      do{
        cout<<"Digite una opcion :";cin>>opcion2;
      }while (opcion2<1 || opcion2>4);
    
      switch (opcion2){
        case 1 :
        case 2 :
        case 3 :
        case 4 :
        default: break;
      }
    } // Fin de ámbito
    break;
    

    Although the best solution would be to extract the content of case to a function. It is better to take good programming habits from the beginning.

    Greetings.

        
    answered by 18.05.2016 в 10:37