Question
How to make this program ask for input again if invalid input is entered (in C programming)? There are two different spots where the check
How to make this program ask for input again if invalid input is entered (in C programming)?
There are two different spots where the check happens.
That is, where you enter a,s,m,d, or q. And, when you enter the first and second number. At any of the checks, if the check is false, it should ask you to re-enter your input. I'm guessing this can be done by putting a scanf statement for the numbers part inside a while loop check, but when I enter an invalid value (non-number) the loop runs infinitely. So I must be doing something wrong. I have made the a,s,m,d, and q part work for the most part. But the second part never seems to work. For this, I left my failed attempts at the while loops out, and instead in comments.
Any help would be greately appreciated!
Here is my code so far:
#include #include
int main(void) { char ch; float num1,num2,answer; printf(\"Enter the operation of your choice: \"); printf(\"a. add s. subtract \"); printf(\"m. multiply q. divide \"); printf(\"q. quit \"); while ((ch = getchar())!='q') { printf(\"Enter the operation of your choice: \"); printf(\"a. add s. subtract \"); printf(\"m. multiply q. divide \"); printf(\"q. quit \"); ch=tolower(ch); if (ch==' ') continue; else { switch(ch) { case 'a': //The code below is what I have tried to make work. //This code would also be copy pasted to the other cases, //of course with the correct operations respectively being used. // //printf(\"Enter first number: \") //while(scanf(\"%f\",&num1)==0) //{ // printf(\"Invalid input. Please enter a number.\"); // scanf(\"%f\",&num1); //} //printf(\"Enter second number: \") //while(scanf(\"%f\",&num2)==0) //{ // printf(\"Invalid input. Please enter a number.\"); // scanf(\"%f\",&num2); //} //answer = num1 + num2; //printf(\"%f + %f = %f \",num1,num2,answer); //break; // //I have also tried to make this work using do-while loops printf(\"Enter first number: \"); scanf(\"%f\",&num1); printf(\"Enter second number: \"); scanf(\"%f\",&num2); answer = num1 + num2; printf(\"%f + %f = %f \",num1,num2,answer); break; case 's': printf(\"Enter first number: \"); scanf(\"%f\",&num1); printf(\"Enter second number: \"); scanf(\"%f\",&num2); answer = num1 - num2; printf(\"%f - %f = %f \",num1,num2,answer); break; case 'm': printf(\"Enter first number: \"); scanf(\"%f\",&num1); printf(\"Enter second number: \"); scanf(\"%f\",&num2); answer = num1 * num2; printf(\"%f * %f = %f \",num1,num2,answer); break; case 'd': printf(\"Enter first number: \"); scanf(\"%f\",&num1); printf(\"Enter second number: \"); scanf(\"%f\",&num2); answer = num1 / num2; printf(\"%f / %f = %f \",num1,num2,answer); break; default: printf(\"That is not a valid operation. \"); break; } } } return 0; }
Again, thanks for any help!
Cheers!
-Will S.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started