samedi 13 mars 2021

why is my switch case not understanding the input from scanf? should i use if else instead?

so this worked just fine with if else statements but I'm wondering what I'm doing wrong I am not sure what is wrong here, It just jumps straight to the default statement for some reason.

int b = true;

do
{
    printf("Press 1 to enter menu\n");
    printf("Press 2 to close program\n");
    printf("> ");

    int c = 0;
    scanf("%i", &c);

    int n = true;
    
    switch(c)
    {
    case '1' :
    
        while (n == true)
        {
            printf("Press 1 to start game\n");
            printf("Press 2 to close program\n");
            printf("Press 3 to see stats\n");
            
            int c1 = 0;
            
        }
        b = false;
        break;

    case '2' :
    
        printf(">>> Closing program <<<\n");
        return 0;
    break;
    
    default :
    
        printf("Try again\n");
    }
}
while(b == true);

}

Aucun commentaire:

Enregistrer un commentaire