How to make this program ask for input again if invalid inpu

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 <stdio.h>
#include <ctype.h>

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.

Solution

//Tested on Windows Os Dev c++

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
/*isNumbercheck function is used to check that s is number or not
*we are comparing ascii value digit value between 48 to 57
*/
bool isNumberCheck(char *s){
   int i;
   bool valid = true;
   for (i = 0; i < strlen(s); i++)
{
if (s[i] > 57 || s[i] < 48)
{
   valid = false;
break;
}
}
  
if(valid==true){
   return true;
}else{
   return false;
}
}
int main(void)
{
char ch;
char num1[10],num2[10];
float answer;
printf(\"Enter the operation of your choice:\ \");
printf(\"a. add s. subtract\ \");
printf(\"m. multiply q. divide\ \");
printf(\"q. quit\ \");
ch = getchar();
fflush(stdin);
while (ch!=\'q\')
{
    fflush(stdin);
printf(\"Enter the operation of your choice:\ \");
printf(\"a. add s. subtract\ \");
printf(\"m. multiply q. divide\ \");
printf(\"q. quit\ \");
ch = getchar();
ch=tolower(ch);
fflush(stdin);
  
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: \");
scanf(\"%s\",&num1);
while(!isNumberCheck(num1))
{
   
printf(\"Invalid input. Please enter a number.\");
scanf(\"%s\",&num1);
}
printf(\"Enter second number: \");
scanf(\"%s\",&num2);
while(!isNumberCheck(num2))
{
printf(\"Invalid input. Please enter a number.\");
scanf(\"%s\",&num2);
}
answer = atof(num1) + atof(num2);
printf(\"%s + %s = %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(\"%s\",&num1);
while(!isNumberCheck(num1))
{
   
printf(\"Invalid input. Please enter a number.\");
scanf(\"%s\",&num1);
}
printf(\"Enter second number: \");
scanf(\"%s\",&num2);
while(!isNumberCheck(num2))
{
printf(\"Invalid input. Please enter a number.\");
scanf(\"%s\",&num2);
}
answer = atof(num1)- atof(num2);
printf(\"%s - %s = %f\ \",num1,num2,answer);
break;
case \'m\':
printf(\"Enter first number: \");
scanf(\"%s\",&num1);
while(!isNumberCheck(num1))
{
   
printf(\"Invalid input. Please enter a number.\");
scanf(\"%s\",&num1);
}
printf(\"Enter second number: \");
scanf(\"%s\",&num2);
while(!isNumberCheck(num2))
{
printf(\"Invalid input. Please enter a number.\");
scanf(\"%s\",&num2);
}
answer = atof(num1)* atof(num2);
printf(\"%s * %s = %f\ \",num1,num2,answer);
break;
case \'d\':
printf(\"Enter first number: \");
scanf(\"%s\",&num1);
while(!isNumberCheck(num1))
{
   
printf(\"Invalid input. Please enter a number.\");
scanf(\"%s\",&num1);
}
printf(\"Enter second number: \");
scanf(\"%s\",&num2);
while(!isNumberCheck(num2))
{
printf(\"Invalid input. Please enter a number.\");
scanf(\"%s\",&num2);
}
answer = atof(num1)/atof(num2);
printf(\"%s / %s = %f\ \",num1,num2,answer);
break;
default:
printf(\"That is not a valid operation.\ \");
break;
}
  
fflush(stdin);
}
return 0;
}

/*****************output****************************/

Enter the operation of your choice:
a. add s. subtract
m. multiply q. divide
q. quit
a
Enter the operation of your choice:
a. add s. subtract
m. multiply q. divide
q. quit
a
Enter first number: 1
Enter second number: 2
1 + 2 = 3.000000
Enter the operation of your choice:
a. add s. subtract
m. multiply q. divide
q. quit
a
Enter first number: 1
Enter second number: d
Invalid input. Please enter a number.3
1 + 3 = 4.000000
Enter the operation of your choice:
a. add s. subtract
m. multiply q. divide
q. quit
s
Enter first number: 56
Enter second number: 34
56 - 34 = 22.000000
Enter the operation of your choice:
a. add s. subtract
m. multiply q. divide
q. quit
m
Enter first number: 12
Enter second number: df
Invalid input. Please enter a number.4
12 * 4 = 48.000000
Enter the operation of your choice:
a. add s. subtract
m. multiply q. divide
q. quit
q
That is not a valid operation.

--------------------------------
Process exited after 35.06 seconds with return value 0
Press any key to continue . . .

Thanks a lot

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, whe
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, whe
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, whe
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, whe
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, whe

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site