In C programming I must find a way to error check the inputs given to me by a user. For example, I am asking the user to input either a 1 or a 0 (yes or no responses) to questions. I have had no problem with that part of the code with error checking if the user inputs a different number. My issue however is when the user inputs an alphabetic or special character, my code goes bad really quick, printing the question an infinite amount of times. So I need a code that can quickly check to see if a user entered either 1 or 0 and not a different number, or some alphabetic or special character.
#include<stdio.h> int main() { char ch; printf("Enter input character: "); scanf("%c",&ch); if(ch=='1'){ printf("It is 1\n"); } else if(ch=='0'){ printf("It is 0\n"); } else if(ch>='2' && ch<='9'){ printf("It is a digit\n"); } else if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z')){ printf("It is an alphabet\n"); } else{ printf("It is a special character\n"); } return 0; }
Get Answers For Free
Most questions answered within 1 hours.