Error Checking Input C
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and c# error checking user input policies of this site About Us Learn more about Stack Overflow the error checking c drive company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users c atoi error checking Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes input validation error a minute: Sign up Check if input is integer type in C up vote 8 down vote favorite 9 The catch is that I cannot use atoi or any other function like that (I'm pretty sure we're supposed to rely on mathematical operations). int num; scanf("%d",&num); if(/* num is not integer */) { printf("enter integer"); return; } I've tried: (num*2)/2 == num
Input Validation Error Types
num%1==0 if(scanf("%d",&num)!=1) but none of these worked. Any ideas? c input integer share|improve this question edited Nov 1 '10 at 19:33 The Archetypal Paul 27.6k1578110 asked Nov 1 '10 at 19:04 Gal 7,1822168104 What do you mean "none of these worked"? And how did you try the last one? –David Thornley Nov 1 '10 at 19:52 @David I placed them in if statement, what is confusing about it? –Gal Nov 1 '10 at 19:54 What kind of input is "integer type"? One consisting only of digits? Is a minus sign allowed in the front? –eq- Nov 1 '10 at 20:05 You list two conditions and an if statement. I assume you didn't include an if statement in an if statement, so I don't know what you did there. (Did you just replace the if statement, or was it more significant than that?) Nor do you provide any clue to failure mode other than "none of these worked". Didn't compile? Run-time error? Didn't work the way you expected (and, if so, what did you expec
statement */ #include
How To Validate Input In C Programming
#include main() { int valid_input; /* when c programming check if input is integer 1, data is valid and loop is exited */ char user_input; /* handles user validate input c input, single character menu choice */ valid_input = 0; while( valid_input == 0 ) { printf("Continue (Y/N)?\n"); scanf(" %c", &user_input ); user_input http://stackoverflow.com/questions/4072190/check-if-input-is-integer-type-in-c = toupper( user_input ); if((user_input == 'Y') || (user_input == 'N') ) valid_input = 1; else printf("\007Error: Invalid choice\n"); } } Sample Program Output Continue (Y/N)? b Error: Invalid Choice Continue (Y/N)? N /* example two, getting and validating choices */ #include #include http://www.gdsw.at/languages/c/programming-bbrown/c_032.htm main() { int exit_flag = 0, valid_choice; char menu_choice; while( exit_flag == 0 ) { valid_choice = 0; while( valid_choice == 0 ) { printf("\nC = Copy File\nE = Exit\nM = Move File\n"); printf("Enter choice:\n"); scanf(" %c", &menu_choice ); if((menu_choice=='C') || (menu_choice=='E') || (menu_choice=='M')) valid_choice = 1; else printf("\007Error. Invalid menu choice selected.\n"); } switch( menu_choice ) { case 'C' : ....................(); break; case 'E' : exit_flag = 1; break; case 'M' : ....................(); break; default : printf("Error--- Should not occur.\n"); break; } } } Sample Program Output C = Copy File E = Exit M = Move File Enter choice: X Error. Invalid menu choice selected. C = Copy File E = Exit M = Move File Enter choice: E Other validation examples © Copyright B Brown. 1984-1999. All rights reserved.
then converting the string to the correct data type. /* example one, to read a word at a time */ #include #include #define MAXBUFFERSIZE 80 void cleartoendofline( void ); /* ANSI function http://www.gdsw.at/languages/c/programming-bbrown/c_055.htm prototype */ void cleartoendofline( void ) { char ch; ch = getchar(); while( ch != https://www.tutorialspoint.com/cprogramming/c_error_handling.htm '\n' ) ch = getchar(); } main() { char ch; /* handles user input */ char buffer[MAXBUFFERSIZE]; /* sufficient to handle one line */ int char_count; /* number of characters read for this line */ int exit_flag = 0; int valid_choice; while( exit_flag == 0 ) { printf("Enter a line of text (<80 chars)\n"); ch = getchar(); char_count = 0; while( (ch error checking != '\n') && (char_count < MAXBUFFERSIZE)) { buffer[char_count++] = ch; ch = getchar(); } buffer[char_count] = 0x00; /* null terminate buffer */ printf("\nThe line you entered was:\n"); printf("%s\n", buffer); valid_choice = 0; while( valid_choice == 0 ) { printf("Continue (Y/N)?\n"); scanf(" %c", &ch ); ch = toupper( ch ); if((ch == 'Y') || (ch == 'N') ) valid_choice = 1; else printf("\007Error: Invalid choice\n"); cleartoendofline(); } if( ch == 'N' ) exit_flag = 1; } } Another input validation error Example, read a number as a string /* example two, reading a number as a string */ #include #include #include #define MAXBUFFERSIZE 80 void cleartoendofline( void ); /* ANSI function prototype */ void cleartoendofline( void ) { char ch; ch = getchar(); while( ch != '\n' ) ch = getchar(); } main() { char ch; /* handles user input */ char buffer[MAXBUFFERSIZE]; /* sufficient to handle one line */ int char_count; /* number of characters read for this line */ int exit_flag = 0, number, valid_choice; while( exit_flag == 0 ) { valid_choice = 0; while( valid_choice == 0 ) { printf("Enter a number between 1 and 1000\n"); ch = getchar(); char_count = 0; while( (ch != '\n') && (char_count < MAXBUFFERSIZE)) { buffer[char_count++] = ch; ch = getchar(); } buffer[char_count] = 0x00; /* null terminate buffer */ number = atoi( buffer ); if( (number < 1) || (number > 1000) ) printf("\007Error. Number outside range 1-1000\n"); else valid_choice = 1; } printf("\nThe number you entered was:\n"); printf("%d\n", number); valid_choice = 0; while( valid_choice == 0 ) { printf("Continue (Y/N)?\n"); scanf(" %c", &ch ); ch = toupper( ch ); if((ch == 'Y') || (ch == 'N') ) valid_choice = 1; else printf("\007Error: Invalid choice\n"); cleartoendofline(); } if( ch == 'N' ) exit_flag = 1; } } Other validation examples ©Copyright B Brown. 1984-1999. All rights reserved.
C - Basic Syntax C - Data Types C - Variables C - Constants C - Storage Classes C - Operators C - Decision Making C - Loops C - Functions C - Scope Rules C - Arrays C - Pointers C - Strings C - Structures C - Unions C - Bit Fields C - Typedef C - Input & Output C - File I/O C - Preprocessors C - Header Files C - Type Casting C - Error Handling C - Recursion C - Variable Arguments C - Memory Management C - Command Line Arguments C Programming Resources C - Questions & Answers C - Quick Guide C - Useful Resources C - Discussion Selected Reading Developer's Best Practices Questions and Answers Effective Resume Writing HR Interview Questions Computer Glossary Who is Who C - Error Handling Advertisements Previous Page Next Page As such, C programming does not provide direct support for error handling but being a system programming language, it provides you access at lower level in the form of return values. Most of the C or even Unix function calls return -1 or NULL in case of any error and set an error code errno. It is set as a global variable and indicates an error occurred during any function call. You can find various error codes defined in header file. So a C programmer can check the returned values and can take appropriate action depending on the return value. It is a good practice, to set errno to 0 at the time of initializing a program. A value of 0 indicates that there is no error in the program. errno, perror(). and strerror() The C programming language provides perror() and strerror() functions which can be used to display the text message associated with errno. The perror() function displays the string you pass to it, followed by a colon, a space, and then the textual representation of the current errno value. The strerror() function, which returns a pointer to the textual representation of the current errno value. Let's try to simulate an error condition and try to open a file which does not exist. Here I'm using both the functions to show the usage, but you can use one or more ways of printing your errors. Second important point to note is that you should use stderr file stream to output all the errors. #include #include #include extern int errno ; int main () { FILE * pf; int errnum; pf = fopen ("unexist.txt", "rb"); if (pf == NULL) { errnum = errno; fprintf(stderr, "Value of errno: %d\n", errno); p