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

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

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

Advertisement

User Input - Problem 2

Write a C-program based on _cgets that asks a user to enter a word from English language that doesn't exceed 5 characters. The program should return -1 if the input is not as per the above requirement. The program should return 0 otherwise.

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 GetConsoleWord ();

main ()
{

    while (0 != GetConsoleWord ())
    {

        printf ("\nYou didnot enter a word of "
        "length <= 5 characters.\n");

    }

}

short GetConsoleWord ()
{

    // 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 an english word of length <= 5 characters: ");

    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 (!isalpha (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 2" by Parveen (Hoven) is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Updated on 2017-05-17.


Advertisement