/Scan

A class similar to Java's Scanner class which verifies user input

Primary LanguageC++

Scan

A class similar to Java's Scanner class which verifies user input.

Installing

Add the class file (scan.hpp) into your include path. There is no need to build the file, since all the functions are defined in the class file itself.

Features-

Verifies the input and throws an exception if not matching.
Uses strict type checking to ensure that doubles and integers are not interchanged.
Takes care of the buffer clearing whenever wrong datatypes are entered.

Note

This class is strict on type-checking, so if the user is expecting an integer using

Scan input;
int x;

x = input.get<int>();

but a double is entered, the class will throw an exception. The same applies for the Scanner::next() function.

It is preferrable to use the Scanner::next() function to avoid unnecessary try blocks-

int x;

x = Scanner::next<int>("Integer expected");
// This gives "Integer expected" as the error message and
// keeps accepting inputs till it encounters an integer

x = Scanner::next<int>();
// This prints out the default message "Invalid datatype entered" as the error

// All error messages are printed to stderr


Demo