simpio.h

This file exports a set of functions that simplify input/output operations in C++ and provide some error-checking on console input.
Functions
getInteger(prompt) Reads a complete line from cin and scans it as an integer.
getLine(prompt) Reads a line of text from cin and returns that line as a string.
getReal(prompt) Reads a complete line from cin and scans it as a floating-point number.
getYesOrNo(prompt) Reads a complete line from cin and treats it as a yes-or-no answer to a question, returning a boolean value of true for yes and false for no.

Function detail


int getInteger(string prompt = "", string reprompt = "");
Reads a complete line from cin and scans it as an integer. If the scan succeeds, the integer value is returned. If the argument is not a legal integer or if extraneous characters (other than whitespace) appear in the string, the user is given a chance to reenter the value. If supplied, the optional prompt string is printed before reading the value.

The also optional reprompt argument provides an output message displayed each time if the user types a file that is not found. If no value is passed, defaults to, "Illegal integer format. Try again.".

Usage:

int n = getInteger(prompt);

double getReal(string prompt = "", string reprompt = "");
Reads a complete line from cin and scans it as a floating-point number. If the scan succeeds, the floating-point value is returned. If the input is not a legal number or if extraneous characters (other than whitespace) appear in the string, the user is given a chance to reenter the value. If supplied, the optional prompt string is printed before reading the value.

The also optional reprompt argument provides an output message displayed each time if the user types a file that is not found. If no value is passed, defaults to, "Illegal numeric format. Try again.".

Usage:

double x = getReal(prompt);

string getLine(string prompt = "");
Reads a line of text from cin and returns that line as a string. The newline character that terminates the input is not stored as part of the return value. If supplied, the optional prompt string is printed before reading the value.

Usage:

string line = getLine(prompt);

bool getYesOrNo(string prompt = "", string reprompt = "");
Reads a complete line from cin and treats it as a yes-or-no answer to a question. Returns true if the line typed begins with a 'y' or 'Y', and returns false if it begins with a 'n' or 'N'. Otherwise the user is given a chance to reenter the value.

If supplied, the optional prompt string is printed before reading the value.

The also optional reprompt argument provides an output message displayed each time if the user types a file that is not found. If no value is passed, defaults to, "Please type a word that starts with 'Y' or 'N'.".

Usage:

if (getYesOrNo(prompt)) { ...

Available since: 2014/02/01 version of C++ library