error in switch case [error] jump to case label [-fpermisive]

1

I am implementing this code which is to project the vowels using a matrix and to identify if a word or phrase is palindrome.

Everything was fine until I made a mistake in the exit option

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;

void gvocales(char vocal);
bool palindromo (string frase);

int main() {
    char op;
    cout<<"*** COMPILACION 02 ***"<<endl;
    do{

    cout<<"Elija la opcion: \n 1-Graficar Vocales. \n 2-Analizador de palindromos. \n 3-salir \n --> \n";
    cin>>op;
    system("cls");


    switch (op)
    {
        case '1':
            cout<<"*** Grafica de vocales ***"<<endl;
            char vocal;
            cout<<"ingrese la vocal: ";
            cin>>vocal;
            cout<<endl;
            gvocales(vocal);
        break;
        case '2':
            cout<<"*** Palindromos ***"<<endl;
            string frase;
            cout<<"ingrese la Frase/Palabra: ";
            cin.ignore();
            getline(cin,frase);
            if(palindromo(frase)){  cout<<endl<<"-"<<frase<<"- es palindromo"<<endl;    }
            else{   cout<<endl<<"-"<<frase<<"- no es palindromo"<<endl; }
        break;
        case '3':
            cout<<"saliendo del programa...";
            break;




         }
    system("pause");
         }while  (op!='3');
}



void gvocales(char vocal)
{
    char matriz[5][5];
        for (int i=0;i<5;i++)
        {
            for (int j=0;j<5;j++){ matriz[i][j]='O';}
        }
    switch (vocal){

    case 'a': 
        cout<<"vocal A"<<endl<<endl;
        for (int i=0;i<5;i++)
        {
            for (int j=0;j<5;j++)
            {
                if(i==0 || i==2 || j==0 || j==4){   cout<<matriz[i][j];     }
                else{   cout<<" ";      }
            }
            cout<<endl;
        }
    break;
    case 'e': 
        cout<<"vocal E"<<endl<<endl;
        for (int i=0;i<5;i++)
        {
            for (int j=0;j<5;j++)
            {
                if(i==0 || i==2 || i==4 || j==0){   cout<<matriz[i][j];     }
                else{   cout<<" ";  }
            }
            cout<<endl;
        }
    break; 
    case 'i': 
        cout<<"vocal I"<<endl<<endl;
        for (int i=0;i<5;i++)
        {
            for (int j=0;j<5;j++)
            {
                if(i==0 || i==4 || j==2){   cout<<matriz[i][j];     }
                else{   cout<<" ";  }
            }
            cout<<endl;
        }
    break;
    case 'o': 
        cout<<"vocal O"<<endl<<endl;
        for (int i=0;i<5;i++)
        {
            for (int j=0;j<5;j++)
            {
                if(i==0 || i==4 || j==0|| j==4) {   cout<<matriz[i][j]; }
                else{   cout<<" ";  }
            }
            cout<<endl;
        }
    break;
    case 'u': 
        cout<<"vocal U"<<endl<<endl;
        for (int i=0;i<5;i++)
        {
            for (int j=0;j<5;j++)
            {
                if(i==4 || j==0|| j==4) {   cout<<matriz[i][j]; }
                else{   cout<<" ";  }
            }
            cout<<endl;
        }
    break;
    }
}



bool palindromo (string frase)
{
    int n= frase.length();
    string aux= "";
    for (int i=0;i<n;i++)
    {
        if(frase[i]!=' ' && frase[i]!=',' && frase[i]!='.')
        {
            aux += frase [i];
        }
    }
    frase=aux;
    n= frase.length();
    for (int i=0;i<n/2;i++)
    {
        if(frase[i]!=frase[n-1-i]) return false;
    }
    return true;
}
    
asked by Yelkin Adrian Rubiano Romero 26.08.2017 в 16:37
source

1 answer

7

Good, the problem is due to the declaration of variables within a case . If you want to declare variables in a case you have to use the {} keys to ensure that the scope ( scope ) of these variables is limited to that case . For example:

switch(op)
{
    case 1: 
    {
       string frase;
       foo();
    }
    break;
    case 2: 
    {
       bar();
    }
    break;
    case 3: 
    {
       exit();
    }
    break;
}    

Therefore, you can choose 2 options:

  • Use {} keys in the cases where you declare variables (or in all).
  • Declare the variables outside the switch.
  • I recommend option 1, since it allows you to have the cleanest and most structured code.

        
    answered by 26.08.2017 в 17:16