-1

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;

  
  • 1
    Welcome to SO. What part of the code are you talking about? With these non-English descriptions most people will not understand directly from the printed menu. Please add your input and also output as well as expected output. "Stopped working" is not a useful problem description. – Gerhardh Dec 07 '20 at 13:25
  • @Gerhardh it is the part with fileopen aka case 3 – mistervoidnewb Dec 07 '20 at 13:26
  • To create a [MCVE](https://stackoverflow.com/help/mcve) you could remove all unrelated code. If you only use 1 case in your test, why would everyone need to read all the other 6 options? – Gerhardh Dec 07 '20 at 13:27
  • you pretty much dont need to understand it, all of the code works but when i implemented file opening it broke the case switching – mistervoidnewb Dec 07 '20 at 13:27
  • You do not have any verification of any function call. **never ever** call `scanf` and related functions without checking the number or parsed parameters. Also **never ever** use `fopen` without checking return value for `NULL`. – Gerhardh Dec 07 '20 at 13:29
  • What does "broke the case switching" mean? Did it execute another case instead? Is the file created? What is the content? – Gerhardh Dec 07 '20 at 13:30
  • for some reason it makes my switch dont work and declares an errorerror: jump to case label -fpermissive in codeblocks – mistervoidnewb Dec 07 '20 at 13:33
  • Note for future question: If you get a compilation error that is not "stopped working". Complie errors and runtime errors are very different. If you have a compiler error please always add the exact error message into the question. – Gerhardh Dec 07 '20 at 13:35
  • This is because you are not allowed to put a label directly at a variable definition. You need an instruction or some block `{}`. – Gerhardh Dec 07 '20 at 13:36
  • @Gerhardh noted, i was suprised i forgot adding it in the post – mistervoidnewb Dec 07 '20 at 13:37
  • Note, it's probably not a good idea to declare variables inside case blocks in C. While it might compile, there are better ways to do this. Either declare prior to `switch()`, or use helper functions in `case :` statement. – ryyker Dec 07 '20 at 14:26
  • wide characters (such as the `á` here: `klávesnici` are illegal when used in string literals (` 14, 40 warning: illegal character encoding in string literals and will cause warnings during compile (`warning: illegal character encoding in string literal`) and undefined behavior during run-time. – ryyker Dec 07 '20 at 14:41
  • Side note: consider replacing the `while` loops with `for` loops. Ex: change `i=1;while(i<8) {...; i++;}` To `for (i = 1; i < 8; i++) {...}` This cleans up the code a bit (1 line instead of 3) and avoids the possibility of forgetting the `i++` in the body of the `while` loop. – 001 Dec 07 '20 at 15:32
  • everyone : the problem started when i inserted code that is inside case 3 before that - normal function and compiling after - error in place where case 4 starts that reads as stated – mistervoidnewb Dec 07 '20 at 15:36
  • Not sure what that last comment means, but the code compiles and runs when brackets are added to case 3. https://godbolt.org/z/TYn6x9 – 001 Dec 07 '20 at 15:48
  • @JohnnyMopp you are right, i just checked it aswell when i got home and all is good – mistervoidnewb Dec 07 '20 at 16:38

1 Answers1

0

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);
    }
Karol Adamiak
  • 141
  • 1
  • 9
  • that could be it i was opperating from tutorials that showed switch case not having brackets, also it worked before that but i understand if this is the right way to do it, it could break thanks to not having brackets and having to open a file – mistervoidnewb Dec 07 '20 at 13:35
  • 1
    Variables cannot be declared in switch-case statement, as you did with "int velk = 8;" For details see https://stackoverflow.com/questions/92396/why-cant-variables-be-declared-in-a-switch-statement – Karol Adamiak Dec 07 '20 at 13:48
  • i did it in other cases and it worked aswell what do you mean cannot be declared – mistervoidnewb Dec 07 '20 at 13:52
  • In case statement, you need to use brackets when declaring new variable. You don't need brackets to change existing variable value or call a function – Karol Adamiak Dec 07 '20 at 14:03
  • 1
    The code compiles with warnings just as it is. BTW, brackets are _not_ required to surround the contents of a `case :` section in a `switch()` statement. They _are_ required however when trying to declare a variable with the same name within multiple `case :` statements. – ryyker Dec 07 '20 at 14:36
  • If you care to edit your answer to address the observations in my previous comment and comment me back I will return to re-evaluate my down-vote. – ryyker Dec 07 '20 at 15:43
  • you were right, after putting it inside them, code works, would have done it sooner if I had time, thanks – mistervoidnewb Dec 07 '20 at 16:32