C ++ input validation

I am starting to program in C ++ and have to do a lot of input validation. I found this feature that seems to be universally applicable, but I am having problems with one aspect; If I typed -90, the program did not give an error. my question (s): 1. How can I add the fact that the input cannot be & lt = 0? 2. Is there a better way to limit user input? Maybe a library in C ++?

Thanks for any help or advice.

#include <ios>  // Provides ios_base::failure
#include <iostream>  // Provides cin

template <typename T>
T getValidatedInput()
{
    // Get input of type T
    T result;
    cin >> result;

    // Check if the failbit has been set, meaning the beginning of the input
    // was not type T. Also make sure the result is the only thing in the input
    // stream, otherwise things like 2b would be a valid int.
    if (cin.fail() || cin.get() != '\n')
    {
        // Set the error state flag back to goodbit. If you need to get the input
        // again (e.g. this is in a while loop), this is essential. Otherwise, the
        // failbit will stay set.
        cin.clear();

        // Clear the input stream using and empty while loop.
        while (cin.get() != '\n')
            ;

        // Throw an exception. Allows the caller to handle it any way you see fit
        // (exit, ask for input again, etc.)
        throw ios_base::failure("Invalid input.");
    }

    return result;
}

Using

inputtest.cpp

#include <cstdlib>  // Provides EXIT_SUCCESS
#include <iostream>  // Provides cout, cerr, endl

#include "input.h"  // Provides getValidatedInput<T>()

int main()
{
    using namespace std;

    int input;

    while (true)
    {
        cout << "Enter an integer: ";

        try
        {
            input = getValidatedInput<int>();
        }
        catch (exception e)
        {
            cerr << e.what() << endl;
            continue;
        }

        break;
    }

    cout << "You entered: " << input << endl;

    return EXIT_SUCCESS;
}
+4
source share
4 answers

std::istream::operator >>defined in terms of strtol, strtouland cousins *, which, unfortunately, always take the minus sign even for unsigned types.

, , , int . std::cin.setf( std::ios::failbit ) , , , .

* operator >> std::num_get, scanf, strto*. , strtoul .

+1

template <typename T>
T getValidatedInput(function <bool(T)> validator) {
    T tmp;
    cin >> tmp;
    if (!validator(tmp)) {
        throw ios_base::failure("Invalid input.");
    }
    return tmp;
}

int input = getValidatedInput<int>([] (int arg) -> bool {
    return arg >= 0;
});
+1
  • unsigned int .
  • , , .
0

I hope this is what you need, it comes out when you enter zero, but displays negative numbers. It throws an exception error due to the I / O method.

#include "stdafx.h"
#include <iostream>

using namespace std;

void inputcatch()
{
    cin.clear();
    cin.ignore(cin.rdbuf()->in_avail());
}

int main()
{
    int input;
    bool quit = false;
    while (!quit)
    {
        cout << "Enter number" << endl;
        cin >> input;
        if (cin.fail())
        {
            inputcatch();
            cout << "incorrect input" << endl;
        }
        else if (input == 0)
        {
            quit = true;

        }
        else
        {
            cout << "your number: " << input << endl;
        }
    }
    return 0;
}
0
source

All Articles