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

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 5 of this series.

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

Advertisement

User Input - Problem 5

Write a C-program based on _cgets that asks a user to enter a number x such that -99999 <= x <= 99999. The program should perform validation 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"
// set maxchars = 7. We are setting it to seven because the maximum
// no. of digits that we are to accept is 5. So if a user enters
// more than 5, we can return failure. We have to have a space
// for the -sign also
#define MAXCHARS    7
// 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;

    printf ("\nEnter a number between -99999 and +99999: ");

    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 string of proper length
    // we should say it is an error
    if (((('-' == cReturn [0]) || ('+' == cReturn [0]))
    &&
    6 < iInputLength)
    ||
    ('-' != cReturn [0]
    &&
    5 < 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 (('-' == cReturn [0]) || ('+' == cReturn [0]))
        continue;

        if (!isdigit (cReturn [iLoopCtr]))
        {

            iRet = -1;

        }

    }

    if (-1 != iRet)
    {

        // convert the input to an int
        *j = atoi (cReturn);

    }

    // 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.



Creative Commons License
This Blog Post/Article "C/C++ Practice Questions on User Input Problem 5" by Parveen (Hoven) is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Updated on 2017-06-24.


Advertisement