0

Hi could anyone please help me? I try to do a code , where after choosing with switch - case, what function you want to run, it gives you opportunity to turn back to "menu" - switch, and the same if you pick wrong item from "menu". Everything works fine, except while function. If does not return to switch but instead it runs "end".

char name[100];
printf("\nWelcome Whats your name?");
fgets(name,sizeof(name),stdin);
printf("\n\nWelcome %s \nThis calculator can do multiple operations \n\t1. addition of matrix\n" ,name);
printf("\t2. dividing of matrix\n\t3. multiplication of matrix\n\t4. transpose of matrix\n\t5. equations\n\t6. complex numbers");
char  q, w; 

do{     
    printf("\nPress: \n\t1 for adding\n\t2 for dividing \n\t3 for multiplying\n\t4 for transpose\n\t5 for solving complex equations\n\t6 for solving complex number operations");
    w=getchar();
    switch(w)          
    {
        case'1':sucet();break;    
        case'2':rozdiel();break;
        case'3':sucin();break;
        case'4':transponovanie();break;
        case'5':rovnica();break;
        case'6':komplexne();break;
        default:printf("\nYou pressed wrong key.\n");break;
    }
    printf("\nIf you want to continue with calculating, press 'a'...\n");     
    printf("\nIf you want to end, press any other key.\n\n");
    q=getchar();
} while (q=='a'|| q=='A');


printf("!!!!!!!!!!!!~~~~~~~~~~~~~~~~~~~~~!!!!!!!!!!!!\n");          
printf("!!!!!!!!!!!!       End           !!!!!!!!!!!!\n");
printf("!!!!!!!!!!!!_____________________!!!!!!!!!!!!\n");


return 0;

}

1 Answer 1

2

When the user chooses an option (w), they have type two characters: the option and enter. You need to "consume" the enter from stdin lest it be assigned to q. Reference this SO post or this one.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .