0

I am having, 2 problems in my code, 1 is that in case 's', my system is not able to take name as input. 2nd is, after executing the full code in case 's', it is automatically printing press s to start and q to quit, then automatically in the next line it is printing, i dont understand, which is the default case statement. Here is the code, and i have attached the problems in the comments.

#include<stdio.h>

int main(){

int PocketMoney, age, rate;
char input;

while(1){
    printf("Press s to start and q to quit\n");
    scanf("%c",&input);

    switch(input){
        case 'q': {
            printf("Quitting the program...\n");
            goto end;
            break;
        }

        case 's': {
            printf("Okay, \n");     //here, as we can't do declaration of anything right after a case, i have added 1 extra line.
            char str[10];
            printf("Enter the name of the child:\n");   //here i dont know why, the system is not taking input of the name
            scanf("%[^\n]%*c", str);
            
            printf("Enter the age of %s\n", str);   //after printing the above line, it is directly printing this line and then taking input of age
            scanf("%d", &age);

            if(age<=10){
                PocketMoney = age*50;
                printf("The pocket money of %s is %d\n", str, PocketMoney);
            }
            else{
                PocketMoney = 500 + (age-10)*75;
                printf("The pocket money of %s at %d years is %d\n", str, age, PocketMoney);
            }
            break;
        }
        default:        // also, here, after executing the code in case 's', it is automatically printing, Press s to start and q to quit
                        //then in the next line, printing, I don't understand(the default statement) and then again printing
            printf("I don't understand\n"); //and then in the nextline, prints Press s to start and q to quit and then starts taking s/q as input
            break;
    }
}
end: 

return 0;

}

0 Answers0