Search code examples
cvariableswarnings

Variable warning set but not used


int none[5];
int ntwo[5];
    
    // the following is in a switch statement...
    
    if (answer == userAnswer)
    {
        printf("Correct!\n");
        score = prevScore + 1;
        prevScore = score;
    }                       
    else
    {
        printf("Incorrect. The correct answer was %d\n\n", answer); 
        none[i] = number1;
        ntwo[i] = number2;
    }
}
break;

// ... Switch statement ends

It gives me an error saying "Variable warning "none" set but not used". I have clearly used it. I dont know why this error i happening. FYI all the other variables you see have been declared. I just took out the imp part where the array appears.


Solution

  • none shows up twice in this code snippet:

    int none[5]; // declared, not set to anything
    

    And then:

    none[i] = number1; // a value has been set, but it's not being used for anything
    

    If, for example, you later had:

    int foo = none[3];  // <-- the value in none[3] is being used to set foo
    

    or

    for(int i = 0; i < 5; i++)
        printf("%d\n", none[i]);   // <-- the values in none are being used by printf
    

    or something to that effect, we would say none is "used", but as the code is, you have: "none" set but not used; exactly what the compiler said.


    In the pastebin link I see your problem:

    You wrote this:

    for(i=0;i<5;i++)
    {
        printf("Question [i]: none[i]+ntwo[i]");
    

    You meant to write this:

    for(i=0;i<5;i++)
    {
        printf("Question [i]: ", none[i]+ntwo[i]);
    

    Now none is being used and your print is doing something useful...