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

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

Last Reviewed and Updated on February 7, 2020
Posted by Parveen(Hoven),
Aptitude Trainer and Software Developer

User Input - Problem 11

The class in the previous question, i.e. in Problem 10, performs cleanup only in the destructor. So if we want to reuse an object of this class for taking input twice, the class will fail because cleanup has not yet taken place. Like shown below. Modify the class so that cleanup can be automatically done.

Solution
int main ()
{

    CConsoleString obj;

    obj.TakeStringInput ();

    printf ("You entered '%s'\n", obj.DisplayStringInput ());

    // one call like the above one is OK but another call
    // like the one below can be problematic because
    // destructor of the object obj has not run yet.
    obj.TakeStringInput ())
    printf ("You entered '%s'\n", obj.DisplayStringInput ());

}

Soln.
// include all the headers
#include "conio.h"
#include "limits.h"
#include <iostream>

class CConsoleBase
{

public:
    // the maximum limit allowed, use a static const
    // initialize here itself
    static const int iMAXCHARS = 10;

protected:
    char m_cBuff [iMAXCHARS + 3 +
    /*take two more so that overflow can be detected*/2];

    char* m_cReturn;

    // add a temporary storage to hold a copy of input string
    // while the cleanup takes place
    char m_cTemp [iMAXCHARS + 3 +
    /*take two more so that overflow can be detected*/2];

    // the length of string entered by the user.
    // this length is required during cleanup when
    // we flush the unwanted characters. so we make it
    // a private member
    int m_iInputLength;

public:
    // constructor
    CConsoleBase ()
    {

        // requirement for _cgets
        m_cBuff [0] = (iMAXCHARS + 2) + 1;

    }

protected:
    // returns success if the user enters a string <= 10 chars
    bool ReadConsoleString ()
    {

        m_cReturn = NULL;

        int iLoopCtr = 0;

        m_cReturn = _cgets (m_cBuff);

        // save length in a variable. Why are we saving it ?
        m_iInputLength = strlen (m_cReturn);

        // save console input on temp so that we can cleanup
        strcpy (this->m_cTemp, this->m_cReturn);

        // Robust programming -
        // remove unwanted, unread characters from the stream
        if (iMAXCHARS == m_iInputLength) _cgets (m_cBuff);

        do
        {

            m_cReturn = _cgets (m_cBuff);

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

        // now write back to m_cReturn, the value of m_cTemp
        strcpy (this->m_cReturn, this->m_cTemp);

        return (m_iInputLength <= iMAXCHARS);

    }

};

class CConsoleString : public CConsoleBase
{

public:
    inline bool TakeStringInput ()
    {

        // read console
        bool bRet = ReadConsoleString ();

        return bRet;

    }

    inline const char* DisplayStringInput ()
    {

        return this->m_cReturn;

    }

};

int main ()
{

    CConsoleString obj;

    printf ("Enter string <= %u chars: ", CConsoleString::iMAXCHARS);

    if (obj.TakeStringInput ())
    {

        printf ("You entered '%s'\n",
        obj.DisplayStringInput ());

    }

    else
    {

        printf ("Truncated input is '%s'\n",
        obj.DisplayStringInput ());

    }

    printf ("Enter string <= %u chars: ", CConsoleString::iMAXCHARS);

    if (obj.TakeStringInput ())
    {

        printf ("You entered '%s'\n",
        obj.DisplayStringInput ());

    }

    else
    {

        printf ("Truncated input is '%s'\n",
        obj.DisplayStringInput ());

    }

    return 0;

}


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 11" 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