Code /* kishio.cpp CIS 250 Dave Klick 2014-01-22 This is the implementation file for a set of utility functions to do input with validation. Updated 2014-02-11 to add: char getChar(const char*, const char*) */ #include "kishio.h" namespace kishio { /* char getChar(const char* prompt, const char* allowed); Arguments: prompt: prompt to display to user allowed: set of allowed characters for input Returns: a valid char entered by the user This function prompts the user and gets a char from the keyboard. If the value entered is not a valid char or is outside the allowed set specified, this function will discard it and re-prompt the user. */ char getChar(const char* prompt, const char* allowed) { char buf[200]; const char* ptr; bool valid = false; while (!valid) { cout << prompt; cin.getline(buf, 200, '\n'); if (strlen(buf) != 1) { cout << "Error: Must be one of: " << allowed << '\n'; } else { ptr = strchr(allowed, (int) buf[0]); if (ptr == NULL) { cout << "Error: Invalid char\n"; } else { valid = true; } } } return buf[0]; } /* double getDouble(const char* prompt, const double min, const double max); Arguments: prompt: prompt to display to user min: the minimum value a user is allowed to enter max: the maximum value a user is allowed to enter Returns: a valid double entered by the user This function prompts the user and gets a double from the keyboard. If the value entered is not a valid double or is outside the range specified by min and max, this function will discard it and re-prompt the user. */ double getDouble(const char* prompt, const double min, const double max) { double value = 0.0; char buf[200]; char *endptr; bool valid = false; double dmin = min; double dmax = max; // swap min and max if they are reversed if (min > max) { dmin = max; dmax = min; } while (!valid) { cout << prompt; cin.getline(buf, 200, '\n'); value = strtod(buf, &endptr); if (strlen(buf) == 0 || (unsigned) (endptr-buf) < strlen(buf)) { cout << "Error: Invalid number\n"; } else { if (value < dmin) { cout << "Error: below minimum value (" << dmin << ")\n"; } else if (value > dmax) { cout << "Error: above maximum value (" << dmax << ")\n"; } else { valid = true; } } } return value; } /* double getDouble(const double min, const double max); Arguments: min: the minimum value a user is allowed to enter max: the maximum value a user is allowed to enter Returns: a valid double entered by the user This function gets a double from the keyboard. If the value entered is not a valid double or is outside the range specified by min and max, this function will discard it and get another double from the user. */ double getDouble(const double min, const double max) { return getDouble("", min, max); } /* double getDouble(const char* prompt); Arguments: prompt: prompt to display to user Returns: a valid double entered by the user This function prompts the user and gets a double from the keyboard. If the value entered is not a valid double this function will discard it and get another double from the user. */ double getDouble(const char* prompt) { return getDouble(prompt, -DBL_MAX, DBL_MAX); } /* double getDouble(); Arguments: none Returns: a valid double entered by the user This function gets a double from the keyboard. If the value entered is not a valid double this function will discard it and get another double from the user. */ double getDouble() { return getDouble("", -DBL_MAX, DBL_MAX); } /* long getLong(const char* prompt, const long min, const long max); Arguments: prompt: prompt to display to user min: the minimum value a user is allowed to enter max: the maximum value a user is allowed to enter Returns: a valid long integer entered by the user This function prompts the user and gets a long integer from the keyboard. If the value entered is not a valid integer or is outside the range specified by min and max, this function will discard it and re-prompt the user. */ long getLong(const char* prompt, const long min, const long max) { long value = 0L; char buf[200]; char *endptr; bool valid = false; long lmin = min; long lmax = max; // swap min and max if they are reversed if (min > max) { lmin = max; lmax = min; } while (!valid) { cout << prompt; cin.getline(buf, 200, '\n'); value = strtol(buf, &endptr, 10); if (strlen(buf) == 0 || (unsigned) (endptr-buf) < strlen(buf)) { cout << "Error: Invalid number\n"; } else { if (value < lmin) { cout << "Error: below minimum value (" << lmin << ")\n"; } else if (value > lmax) { cout << "Error: above maximum value (" << lmax << ")\n"; } else { valid = true; } } } return value; } /* long getLong(const long min, const long max); Arguments: min: the minimum value a user is allowed to enter max: the maximum value a user is allowed to enter Returns: a valid long integer entered by the user This function gets an integer from the keyboard. If the value entered is not a valid long integer or is outside the range specified by min and max, this function will discard it and get another integer from the user. */ long getLong(const long min, const long max) { return getLong("", min, max); } /* long getLong(const char* prompt); Arguments: prompt: prompt to display to user Returns: a valid long integer entered by the user This function prompts the user and gets an integer from the keyboard. If the value entered is not a valid long integer this function will discard it and get another integer from the user. */ long getLong(const char* prompt) { return getLong(prompt, LONG_MIN, LONG_MAX); } /* long getLong(); Arguments: none Returns: a valid long integer entered by the user This function gets an integer from the keyboard. If the value entered is not a valid long integer this function will discard it and get another integer from the user. */ long getLong() { return getLong("", LONG_MIN, LONG_MAX); } /* int getInt(const char* prompt, const int min, const int max); Arguments: prompt: prompt to display to user min: the minimum value a user is allowed to enter max: the maximum value a user is allowed to enter Returns: a valid integer entered by the user This function prompts the user and gets an integer from the keyboard. If the value entered is not a valid integer or is outside the range specified by min and max, this function will discard it and re-prompt the user. */ int getInt(const char* prompt, const int min, const int max) { long value = 0L; char buf[200]; char *endptr; bool valid = false; int imin = min; int imax = max; // swap min and max if they are reversed if (min > max) { imin = max; imax = min; } while (!valid) { cout << prompt; cin.getline(buf, 200, '\n'); value = strtol(buf, &endptr, 10); if (strlen(buf) == 0 || (unsigned) (endptr-buf) < strlen(buf)) { cout << "Error: Invalid number\n"; } else { if (value < imin) { cout << "Error: below minimum value (" << imin << ")\n"; } else if (value > imax) { cout << "Error: above maximum value (" << imax << ")\n"; } else { valid = true; } } } return (int) value; } /* int getInt(const int min, const int max); Arguments: min: the minimum value a user is allowed to enter max: the maximum value a user is allowed to enter Returns: a valid integer entered by the user This function gets an integer from the keyboard. If the value entered is not a valid nteger or is outside the range specified by min and max, this function will discard it and get another integer from the user. */ int getInt(const int min, const int max) { return getInt("", min, max); } /* int getInt(const char* prompt); Arguments: prompt: prompt to display to user Returns: a valid integer entered by the user This function prompts the user and gets an integer from the keyboard. If the value entered is not a valid integer this function will discard it and get another integer from the user. */ int getInt(const char* prompt) { return getInt(prompt, INT_MIN, INT_MAX); } /* int getInt(); Arguments: none Returns: a valid integer entered by the user This function gets an integer from the keyboard. If the value entered is not a valid integer this function will discard it and get another integer from the user. */ int getInt() { return getInt("", INT_MIN, INT_MAX); } } // end of kishio namespace