Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

Safely get console input

Display your little helpful snippets of code that may help us all get something done

Safely get console input

Postby C++ on Mon Sep 22, 2008 10:28 am

I wrote this to get any type of input safely from the console. If you only use cin >> some_integral_variable, then there's a chance a user will enter characters instead of numbers. The stream will go into a fail state and there's a chance your program won't work like expected. This function gets valid input for any built in type. When you create user defined types, you can also use this function to get input from within that user defined type's overloaded>> operator function.

This function basically makes sure that the stream is not in a fail state and that there are no invalid characters still in the input stream. I included some examples on how to use the function for different data types. I hope it's helpful to you :)

Code: Select all
#include <iostream>
#include <string>
#include <climits>


template <typename T>
T getInput(const char *str = "");

template <typename T>
void getInput(T &, const char *str = "");

template <>
std::string getInput(const char *);

template <>
void getInput(std::string &, const char *);

int main()
{
    char cA = getInput<char>("Enter a character: ");
    short sA = getInput<short>("Enter a short integer: ");
    int nA = getInput<int>("Enter an integer: ");
    long lA = getInput<long>("Enter a long integer: ");
    float fA = getInput<float>("Enter a float number: ");
    double dA = getInput<double>("Enter a double number: ");
    long double ldA = getInput<long double>("Enter a long double number: ");
    std::string strA = getInput<std::string>("Enter a string: ");

    char cB;
    getInput(cB, "Enter a character: ");
    short sB;
    getInput(sB, "Enter a short integer: ");
    int nB;
    getInput(nB, "Enter an integer: ");
    long lB;
    getInput(lB, "Enter a long integer: ");
    float fB;
    getInput(fB, "Enter a float number: ");
    double dB;
    getInput(dB, "Enter a double number: ");
    long double ldB;
    getInput(ldB, "Enter a long double number: ");
    std::string strB;
    getInput(strB, "Enter a string: ");

    std::cout << "\n" << cA << ", " << sA << ", " << nA << ", " << lA << ", " << fA << ", " << dA << ", " << ldA << "\n";
    std::cout << strA << "\n";
    std::cout << "\n" << cB << ", " << sB << ", " << nB << ", " << lB << ", " << fB << ", " << dB << ", " << ldB << "\n";
    std::cout << strB << "\n";
}

template <typename T>
T getInput(const char *str = "")
{
    T input;
    for (;;)
    {
        std::cout << str;
        if (!(std::cin >> input).fail()) break;
        else std::cout << "Invalid input. Please try again.\n\n";
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    return input;
}

template <typename T>
void getInput(T &input, const char *str = "")
{
    for (;;)
    {
        std::cout << str;
        if (!(std::cin >> input).fail()) break;
        else std::cout << "Invalid input. Please try again.\n\n";
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

template<>
std::string getInput(const char *str)
{
    std::string input;
    std::cout << str;
    getline(std::cin, input);
    return input;
}

template<>
void getInput(std::string &input, const char *str)
{
    std::cout << str;
    getline(std::cin, input);
}
New to C++ programming? Click here
C++
teh awesome
 
Posts: 217
Joined: Sun May 25, 2008 7:45 am

Re: Safely get console input

Postby glinka57 on Tue Mar 24, 2009 6:05 pm

seems bulky, I'll check it out later
User avatar
glinka57
 
Posts: 158
Joined: Fri Feb 27, 2009 7:32 pm


Return to Snippets

Who is online

Users browsing this forum: No registered users and 0 guests