C/C++ Practice Questions on User Input Problem 6

This is my series on C/C++ problems for accepting user input in a robust way. The questions of this series start from C programs, and they slowly build and convert to classes, and which further build into inheritance. This is Problem 6 of this series.

Last Reviewed and Updated on May 17, 2017
Posted by Parveen(Hoven),
Aptitude Trainer and Software Developer

User Input - Problem 6

Write a C-program based on _cgets that asks a user to enter a number x such that -2147483647 <= x <= 2147483647. Here 2147483647 is same as INT_MAX, the absoulte value of the largest signed int on MSVC 2008 compiler. On some compilers it could be 65535. so the validation should be regarding INT_MAX.

The program should perform validation on range and return true[or -1] if the input satisfies the above criterion. Otherwise it should return false[or 0].

For this you should write a function with the following declaration

```short GetConsoleInt (int* j);

```

Here the return is -1 if the function succeeds, otherwise it would be 0, and int*is the address of an int which would contain the validated input.

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 x;

if (0 == GetConsoleInt (&x))
printf ("You entered: %d\n\n", x);

else
printf ("Your input is not valid\n\n");

}

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;

}

```