2 votes

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

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

Everything was going well until I got an error in the output 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;
}

12voto

DanielGS Points 797

Good, the problem is due to the declaration of variables inside a case . If you want to declare variables in a case you have to use the keys {} to ensure that the range ( 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:

  1. Using keys {} in the cases where you declare variables (or in all).
  2. Declare variables outside the switch.

I recommend option 1, since it allows you to have a cleaner and more structured code.

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X