ccodeblocksfopenfile-handlingfclose

Error when implementing file printing in project Code:Blocks C


Hey so I have this project I have been working on and everything works but when I tried making one of the options user gets to by switch:case the option to save data to a file it stopped working (the switch did) I dont understand why since I checked many tutorials and my code is same as what they have for some reason it makes my switch dont work and declares an error: jump to case label -fpermissive in codeblocks

int main()
{
    double trec [8] = {0,0,0,0,0,0,0,0};
    
    int i;
    int a;
    double exp;
    FILE *fp;

    ;

  
    switch(a)
    {
    case 0:
        exit (0);

    break;

  

    case 3:
        int velk = 8;
        i = 0;
        fp=fopen("text.txt", "w+");
        //for(int i = 0; i < velk; ++i){
        //   fprintf(fp, "%lf ", trec[i]);
        //}
        fprintf(fp, "%d  %lf",1 , trec[i]);
        fclose(fp);
    break;

    case 4:
     i = 1;
     while(i<8){
        printf(" %s its %lf",day[i], trec[i]);
        i++;
    }
    break;

  

Solution

  • Your code does not compile due to missing brackets { } in switch-case:

    case 3:
        {
            int velk = 8;
            i = 0;
            fp=fopen("text.txt", "w+");
            //for(int i = 0; i < velk; ++i){
            //   fprintf(fp, "%lf ", trec[i]);
            //}
            fprintf(fp, "%d  %lf",1 , trec[i]);
            fclose(fp);
        }