help defining char within if in C

0
Hello, I'm trying to set the char variable filename [100] inside the if, but the error in the compiler, but if I set it outside the if it works fine, any help pls? (#defines are stdio.h stdlib.h time.h and locale.h)

void selecionarPersonagem(int opcaoPersonagem);

char tipoPersonagem[3][100] = {{"Lutador"}, {"Ninja"}, {"Apelão"}};
char tipoArma[3][100] = {{"Faca"}, {"Pistola"}, {"Socão"}};

int main()
{
    //Definição para acentos e cedilha
    setlocale(LC_ALL, "Portuguese");

    int flag = 0;
    do{
        int opcaoPersonagem;
        printf("Escolha seu personagem:\n");
        printf("[1] All Might.\n");
        printf("[2] Bulma.\n");
        printf("[3] Kirito.\n");
        printf("[4] Naruto.\n");
        scanf("%i", &opcaoPersonagem);
        if(opcaoPersonagem < 1 || opcaoPersonagem > 4)
            printf("Opção inválida, tente novamente.");
        else{
        selecionarPersonagem(opcaoPersonagem);
        }
    }while(flag == 0);

    return 0;
}

void selecionarPersonagem(int opcaoPersonagem)
{
    FILE *fptr;
    char c;
    if(opcaoPersonagem == 1)
        char filename[100] = "personagem1.txt"
    fptr = fopen(filename, "r");
    c = fgetc(fptr);
    while(c != EOF){
        printf("%c", c);
        c = fgetc(fptr);
    }
    fclose(fptr);
}

Compiler error: || === Build: Debug in project-mini-rpg (compiler: GNU GCC Compiler) === | C: \ Users \ pauli.dev \ c \ project-mini-rpg \ main.c || In function 'selectSize': | C: \ Users \ pauli.dev \ c \ project-mini-rpg \ main.c | 40 | error: expected expression before 'char' | || === Build failed: 1 error (s), 0 warning (s) (0 minute (s), 0 second (s)) === |

    
asked by anonymous 30.05.2018 / 02:15

1 answer

1
if(opcaoPersonagem == 1){
    FILE *fptr;
    char c;
    char filename[100] = "personagem1.txt";
    fptr = fopen(filename, "r");
    c = fgetc(fptr);
    while(c != EOF){
        printf("%c", c);
        c = fgetc(fptr);
    }
    fclose(fptr);
}

The error you're pointing to is another place, filename was not identified in fopen(filename, "r"); . You have a static semantic error, the filename variable must be declared in the same scope that you are using. The if without the open and close keys only considers the first statement afterwards.

    
30.05.2018 / 02:30