User Input - Problem 7
Write a C-program based on _cgets that asks a user to enter two int. The program should perform validation on range, type and ask the user to re-enter till a valid number is entered. Finally, display the sum of the two numbers. Also perform validation on the sum.
Solution// include all the headers #include "conio.h" #include "string.h" #include "ctype.h" #include "stdlib.h" #include "errno.h" // set maxchars = 12. We are setting it to 12 because the maximum // no. of digits that we are to accept is 10. So if a user enters // more than 10, we can return failure. We have to have a space // for the -sign also #define MAXCHARS 12 // declare the function short GetConsoleInt (int* j); main () { int iFirst, iSecond; while (1) { while (0 != GetConsoleInt (&iFirst)); while (0 != GetConsoleInt (&iSecond)); // check if the sum is in range if (iFirst > INT_MAX - iSecond) { printf ("The sum would overflow. " "Re-enter numbers.\n\n"); } else break; } printf ("Sum of two numbers is: %d\n", iFirst + iSecond); } short GetConsoleInt (int* j) { // set a buffer for saving six digits. buffer should be n + 3 char cBuff [MAXCHARS + 3]; char* cReturn = NULL; int iInputLength = 0, iLoopCtr = 0; short iRet = 0; cBuff [0] = MAXCHARS + 1; // the printf is now modular with INT_MAX printf ("Enter a number between -%d and +%d: ", INT_MAX, INT_MAX); cReturn = _cgets (cBuff); // save the length in a variable. Why are we saving it ? // we are saving it because strlen is a function which has an // overhead on each call. so if we are going to call strlen // many times[in a 'for' loop, for example] then it could // be too costly. so it is better to save it. iInputLength = strlen (cReturn); // if the user has not entered a proper string // we should say it is an error, we are taking the length = 10 // but for 64-bit systems this won't work because // INT_MAX could be longer if (((('-' == cReturn [0]) || ('+' == cReturn [0])) && 11 < iInputLength) || ('-' != cReturn [0] && 10 < iInputLength) || (0 == iInputLength) ) { iRet = -1; } // now check each digit, do checking so long as iRet != -1 for ( iLoopCtr = 0; iLoopCtr < iInputLength && -1 != iRet; iLoopCtr++) { // if the user enters a sign, we should ignore it // in our validation. our purpose is to check for // valid numbers if (('-' == cReturn [0]) || ('+' == cReturn [0])) { continue; } if (!isdigit (cReturn [iLoopCtr])) { iRet = -1; } } if (-1 != iRet) { // now verify if the number is within the range. // the range as you can see is INT_MAX. so *j = atoi (cReturn); // atoi sets errno to ERANGE if out of range occurs // the header errno.h is include at the top for this if (ERANGE == errno) { iRet = -1; _set_errno (0); } } // Robust programming - // remove any unwanted, unread characters from the stream if (MAXCHARS == iInputLength) _cgets (cBuff); do { cReturn = _cgets (cBuff); }while (0 != cReturn [0]); return iRet; }
My C/C++ Videos on Youtube
Here is the complete playlist for video lectures and tutorials for the absolute beginners. The language has been kept simple so that anybody can easily understand them. I have avoided complex jargon in these videos.
This Blog Post/Article "C/C++ Practice Questions on User Input Problem 7" by Parveen (Hoven) is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Updated on 2020-02-07. Published on: 2015-12-20