help setting char inside the if in C

Asked

Viewed 109 times

0

Hello, I’m trying to set the char variable filename[100] inside if, but the compiler error, but if I set it outside of if it works good, any pls help? (#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);
}

Error giving compiler: ||=== Build: Debug in projeto-mini-rpg (Compiler: GNU GCC Compiler) ===| C: Users Pauli.dev c project-mini-rpg main. c||In Function 'selectPersonagem':| 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)) ===|

  • What is line 40? FILE *fptr;?

  • char filename[100] = "personagem1.txt"

  • Ever tried to use the open and close quotes? , and missed the bridge and comma after

  • is already in quotes, I put the point and comma at the end and continues the same mistake

  • trial {const char *filename = "personagem1.txt";}

  • is giving the same error yet http://prntscr.com/jobf4k

Show 1 more comment

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 mistake you’re pointing to is somewhere else, filename was not identified in fopen(filename, "r");. You have a static semantic error, the variable filename has to be declared in the same scope as is being used. if without opens and closes keys only considers the first instruction after.

  • 1

    po, it worked bro, thanks, but there’s no way to do it the way I tried there? because I have to repeat 4 times, gave 50 lines only of this. Although being in the void still remains many lines ;-;

  • 1

    This makes no difference to the performance, it is even better to leave separate, because it is easier to give maintenance. You would need at least 500,000 lines to slow down

  • thanks, to well in basic yet, so I have not much idea to be heavy or not

Browser other questions tagged

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