char - Switch Statement Not Executing Cases (c) -
i made switch statement, however, works constants set. if try use user input, 1 of cases work, every other 1 doesn't. no matter enter, uses default case. tried adding getchar() clear \n character buffer isn't making difference. ill post entire switch statement here :
char option=' '; option=getchar(); switch(option){ //parallel resistance calculations case 'p': clear //presistance(); printf("resistance"); getchar(); break; //ohm's law calculations case 'o': clear printf("ohm"); //ohm(); break; //exits program case 'q': printf("good bye! stay safe in laboratory! :)\npress key exit"); getchar(); exit(0); break; //error checking default : printf("invalid input, try again"); break; } } while (option!='q'); i commented out functions use print statements test if working.
whenever input character or string stdin in c, make sure there no \n in input buffer. this, getchar() after taking integer or float inputs.
in case, maybe you've inputted integer before inputting character. try write getchar() before taking character input.
Comments
Post a Comment