Home Template for handling different type of input
Reply: 0

Template for handling different type of input

user4209
1#
user4209 Published in April 25, 2018, 6:03 pm

I was looking at this SO post on writing template for reading any type of user input. I wrote template below. But problem starts when string input is given in place of character or floating point number is given in place of integer. When floating number is given as input to the integer, only data before the decimal point gets assigned to the variable. For example: When we assign 6.7 to integer, 6 gets assigned, but .7 is left in input buffer and . gets automatically assigned to next character without waiting for user input. How do we handle this? I cannot call std::cin.clear() unless there is something in input buffer to clear. Calling std::cin.clear() when correct data is entered causes indefinite wait for some character.

#include <iostream>
#include <limits>
using namespace std;

template <typename myType>
void getInput(myType &data, const string& promptMessage)
{
    std::cout<<promptMessage;
    std::cin>>data;
    while(std::cin.fail())
    {
        std::cin.clear();
        cin.ignore(std::numeric_limits<int>::max(),'\n');
        cout<<"Entered Invalid data, Re-Enter: \n";
        std::cin>>data;
    }

}

void getInput(string &data, const string& promptMessage)
{
    std::cout<<promptMessage;
    getline(std::cin,data);
}

int main(void)
{
    int myInt;
    //cout<<"Enter an Integer : ";
    getInput(myInt, "Enter the Integer Data: ");
    cout<<"Integer Value read = "<<myInt<<endl;

    char myChar;
    getInput(myChar, "Enter single Character: ");
    cout<<"Character read = "<<myChar<<endl;
    return(0);
}

Edit2: After looking at replies, updated the template function below. Able to handle floating point or integer. But I am not able to handle characters and strings in the sample template function. I can definitely go for overloaded function, but I guess that defeats the purpose.

#include <iostream>
#include <limits>
#include <type_traits>
#include <stdlib.h>
#include <string>

template <typename myType>
void getInput(myType &data, const string& promptMessage)
{
    string str;
    char *ret;
    uint8_t status=0;
    std::cout<<promptMessage;
    while(!status)
    {
        do
        {
            std::getline(std::cin,str);
        }
        while(str.empty());

        if(std::is_integral <myType>::value)
        {
            data = strtoll(str.c_str(),&ret,10);
            if(*ret=='\0')
            {
                status=1;
            }
            else
            {
                std::cout<<"Invalid data, Please re-enter"<<endl;
            }
        }

        /** Check for floating point data */
        else if(std::is_floating_point <myType>::value)
        {
            data = strtold(str.c_str(),&ret);
            if(*ret=='\0')
            {
                status=1;
            }
            else
            {
                std::cout<<"Invalid data, Please re-enter"<<endl;
            }
        }
        else
        {
            // Need to see how to assign string type to template type
            // Otherwise we still need to handle this else part
        }
    }
}

int main(void)
{
    int myInt;
    //cout<<"Enter an Integer : ";
    getInput<int>(myInt, "Enter the Integer Data: ");
    cout<<"Integer Value read = "<<myInt<<endl;
    long long myLong;
    //cout<<"Enter an Integer : ";
    getInput<long long>(myLong, "Enter the long long Data: ");
    cout<<"Long Long Value read = "<<myLong<<endl;

    char myChar;
    getInput <char> (myChar, "Enter single Character: ");
    cout<<"Character read = "<<myChar<<endl;

    double dbl;
    getInput <double> (dbl, "Enter double value: ");
    cout<<"double value = "<<dbl<<endl;

    string myArr;
    getInput(myArr, "Enter the string ");
    cout<<"String read = "<<myArr<<endl;
}

The output is here.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.329195 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO