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

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

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

Advertisement

User Input - Problem 1

Write a C-program based on _cgets that asks a user to enter a five digit number. The program should return 0 only if the input is a number with exactly five digits.

Solution
// include all the headers
#include "conio.h"
#include "string.h"
#include "ctype.h"
// set maxchars = 6. We are setting it to six 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
#define MAXCHARS    6
// declare a function
short GetConsoleInput ();

main ()
{

    while (0 != GetConsoleInput ())
    {

        printf ("\nYou didnot enter a five digit number.\n");

    }

}

short GetConsoleInput ()
{

    // set a buffer for saving six digits. buffer should be n + 3
    char cBuff [MAXCHARS + 3];

    char* cReturn = NULL;

    int iInputLength = 0, j, iRet = 0;

    cBuff [0] = MAXCHARS + 1;

    printf ("\nEnter the 5-digit number: ");

    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 == 5 digits
    // we should say it is an error
    if (5 != iInputLength)
    {

        iRet = -1;

    }

    // now check each digit
    for (j = 0; j < iInputLength; j++)
    {

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

            iRet = -1;

            // good practice, break out of the loop
            // if we have detected a non-digit, why
            // would we loop unnecessarily ?
            break;

        }

    }

    if (-1 == iRet)
    {

        // Robust programming: remove any
        // unwanted, unread characters from the stream
        if (MAXCHARS == iInputLength) _cgets (cBuff);

        do
        {

            cReturn = _cgets (cBuff);

        }while (0 != cReturn [0]);

    }

    else
    {

        // if we are at this point then it means that
        // the user has entered exactly 5-digtis and
        // all of them are digits, so we inform him.
        printf ("\nYou entered: %s\n", cReturn);

    }

    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 1" by Parveen (Hoven) is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Updated on 2017-06-24.


Advertisement