languages

A collection of programs made with different programming languages.
git clone git://evanalba.com/languages
Log | Files | Refs

CinReader.h (8988B)


      1 /*
      2 Copyright (c) 2008, J Boyd Trolinger
      3 
      4 All rights reserved.
      5 
      6 Redistribution and use in source and binary forms, with or without modification, 
      7 are  permitted provided that the following conditions are met:
      8 
      9 Redistributions of source code must retain the above copyright notice, this list 
     10 of conditions and the following disclaimer.
     11 
     12 Redistributions in binary form must reproduce the above copyright notice, this list 
     13 of conditions and the following disclaimer in the documentation and/or other materials 
     14 provided with the distribution.
     15 
     16 Neither the name of the Butte College Department of Computer Science nor the names of its 
     17 contributors may be used to endorse or promote products derived from this software without 
     18 specific prior written permission.
     19 
     20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     24 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     25 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     26 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     27 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     28 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     29 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31 */
     32 
     33 #pragma once
     34 
     35 #include <iostream>
     36 #include <string>
     37 #include <algorithm>
     38 #include <climits>
     39 #include <sstream>
     40 
     41 using namespace std;
     42 
     43 /**
     44  *   A class for handling keyboard input.  CinReader provides
     45  *   functions for reading all of the C++ primitive data types
     46  *   and C++ strings, and performs error checking on the input.
     47  *   @author J Boyd Trolinger
     48  *   @version 1.1
     49  */
     50  class CinReader
     51 {
     52 	public:
     53 	
     54 		/**
     55 		 *  Constructor.
     56 		 *  Initializes data members to the following values:
     57 		 *  <ul>
     58 		 *  <li>intErrors = true;</li>
     59 		 *  <li>charRangeErrors = true;</li>
     60 		 *  <li>boolMsg = "Please enter \"true\" or \"false\": "</li>
     61 		 *  <li>charMsg = "Please enter a single character: "</li>
     62 		 *  <li>charRangeMsg = "is not valid. Re-enter: "</li>
     63 		 *  <li>intMsg = "Re-enter number: "</li>
     64 		 *  <li>doubleMsg = "Input is not a double. Re-enter: "</li>
     65 		 *  <li>floatMsg = "Input is not a float. Re-enter: "</li>
     66 		 *  <li>stringMsg = "Input cannot be blank--enter text: "</li>
     67 		 *  </ul>
     68 		 */
     69 		CinReader ();
     70 		
     71 		/**
     72 		 *  Destructor.
     73 		 *  CinReader performs no dynamic memory allocation.  Destructor
     74 		 *  is provided for thoroughness and to reinforce good OO/C++ 
     75 		 *  programming practices.
     76 		 */
     77 		~CinReader ();
     78 		
     79 		/**
     80 		 *  Read integer input from the keyboard.
     81 		 *  Used with no arguments, the function will return an integer between
     82 		 *  INT_MIN and INT_MAX.  Optionally the function can be called
     83 		 *  with caller-specified upper and lower limits. If lower>upper, valid return
     84 		 *  value is not guaranteed. Uses intMsg for error prompt.
     85 		 * @param lower caller-specified lower limit of the input, defaults to INT_MIN
     86 		 * @param upper caller-specified upper limit of the input, defaults to INT_MAX
     87 		 * @return an integer between INT_MIN and INT_MAX if called with
     88 		 *               no arguments, or between lower and upper  (inclusive)
     89 		 */
     90 		int readInt (int lower=INT_MIN, int upper=INT_MAX);
     91 		
     92 		/**
     93 		 *  Read integer input from the keyboard.
     94 		 *  Provided for backward compatibility with the version 1.0 CinReader API. Calls
     95 		 *  readInt(lower,upper) if userLimit==true, else calls readInt().
     96 		 *  @param userLimit if true, limit keyboard input to caller-specified range
     97 		 *  @param lower caller-specified lower limit of the input
     98 		 *  @param upper caller-specified upper limit of the input
     99 		 *  @return an integer between lower and upper (inclusive)
    100 		 */
    101 		int readInt (bool userLimit, int lower, int upper);
    102 		
    103 		/**
    104 		 *  Read double input from the keyboard.
    105 		 *  Unlike readInt, this function does not limit the range of the
    106 		 *  input value. Uses doubleMsg for error prompt.
    107 		 *  @return a double
    108 		 */
    109 		double readDouble ();
    110 		
    111 		/**
    112 		 *  Read float input from the keyboard.
    113 		 *  Unlike readInt, this function does not limit the range of the
    114 		 *  input value. Uses floatMsg for error prompt.
    115 		 *  @return a float
    116 		 */
    117 		float readFloat ();
    118 		
    119 		/**
    120 		 *  Read input from the keyboard as a boolean value.
    121 		 *  Will accept "T", "F" or "TRUE", "FALSE" as input and will
    122 		 *  return a corresponding boolean value.  The function is NOT
    123 		 *  case sensitive, it will accept input as any combination of
    124 		 *  uppercase and lowercase characters. Uses boolMsg for error prompt.
    125 		 *  @return a bool
    126 		 */
    127 		bool readBool ();
    128 		
    129 		/**
    130 		 * Read char input from the keyboard.
    131 		 *  Used with no arguments, the function will return the char entered at 
    132 		 *  the keyboard.  Optionally, a range of acceptable inputs can
    133 		 *  be specified.  The range must be written as a list of chars, such as
    134 		 *  "abcdef". Uses charMsg for error prompt. If charRangeErrors==true, and 
    135 		 *  range.length() > 0, also uses charRangeMsg for error prompt.
    136 		 *  @param range the range of acceptable inputs
    137 		 *  @return a char that is a member of range if specified, or
    138 		 *                 any single char if no range is provided
    139 		 */
    140 		char readChar (string range="");
    141 		
    142 		/**
    143 		 *  Read string input from the keyboard.
    144 		 *  Used with no arguments, the function will return the string
    145 		 *  entered at the keyboard, which can include an empty string.
    146 		 *   Uses stringMsg for error prompt.
    147 		 *  @param allowEmpty if true, allow empty string as input, else
    148 		 *                 require at least one character of input
    149 		 *  @param limitTo if 0, do not limit the number of characters of
    150 		 *                 input; if not 0, return only "limitTo" number of
    151 		 *                 characters
    152 		 *  @return a string
    153 		 */
    154 		string readString (bool allowEmpty = true, unsigned int limitTo = 0);
    155 		
    156 		/**
    157 		 * Enable or disable more verbose error messages for invalid integer inputs.
    158 		 * Defaults to true.
    159 		 * @param show 	if true, show additional ERROR output on invalid input; if false,
    160 		 * 					display only intMsg as error prompt.
    161 		 */
    162 		void showIntErrors (bool show);
    163 		
    164 		/**
    165 		 * Enable or disable more verbose error messages for invalid character inputs when 
    166 		 * limiting to a caller-specified range.
    167 		 * Defaults to true.
    168 		 * @param show 	if true, show additional ERROR output on invalid input; if false,
    169 		 * 					display only charMsg as error prompt.
    170 		 */
    171 		void showCharRangeErrors (bool show);
    172 		 
    173 		/**
    174 		 * Set the prompt string for invalid bool inputs.
    175 		 * Default boolMsg is "Please enter \"true\" or \"false\": "
    176 		 * @param newMessage a prompt string for invalid bool inputs
    177 		 */
    178 		void setBoolMessage (string newMessage);
    179 		
    180 		/**
    181 		 * Set the prompt string for invalid char inputs.
    182 		 * Default charMsg is "Please enter a single character: "
    183 		 * @param newMessage a prompt string for invalid char inputs
    184 		 */
    185 		void setCharMessage (string newMessage);
    186 		
    187 		/**
    188 		 * Set the prompt string for invalid char inputs when limiting to a caller-specified 
    189 		 * range.
    190 		 * Default charRangeMsg is "[CHAR] is not valid. Re-enter: " (CHAR is the user's invalid
    191 		 * input.) "[CHAR]" is always appended to the beginning of the char range error prompt string.
    192 		 * @param newMessage a prompt string for invalid char inputs
    193 		 */
    194 		void setCharRangeMessage (string newMessage);
    195 		
    196 		/**
    197 		 * Set the prompt string for invalid int inputs.
    198 		 * Default intMsg is "Re-enter number: "
    199 		 * @param newMessage a prompt string for invalid int inputs
    200 		 */
    201 		void setIntMessage (string newMessage);
    202 		
    203 		/**
    204 		 * Set the prompt string for invalid double inputs.
    205 		 * Default doubleMsg is "Input is not a double. Re-enter: "
    206 		 * @param newMessage a prompt string for invalid double inputs
    207 		 */
    208 		void setDoubleMessage (string newMessage);
    209 		
    210 		/**
    211 		 * Set the prompt string for invalid float inputs.
    212 		 * Default floatMsg is "Input is not a float. Re-enter: "
    213 		 * @param newMessage a prompt string for invalid float inputs
    214 		 */
    215 		void setFloatMessage (string newMessage);
    216 		
    217 		/**
    218 		 * Set the prompt string for invalid string inputs.
    219 		 * Default stringMsg is "Input cannot be blank--enter text: "
    220 		 * @param newMessage a prompt string for invalid string inputs
    221 		 */
    222 		void setStringMessage (string newMessage);
    223 		
    224 	private:
    225 		
    226 		bool intErrors;
    227 		bool charRangeErrors;
    228 		string boolMsg;
    229 		string charMsg;
    230 		string charRangeMsg;
    231 		string intMsg;
    232 		string doubleMsg;
    233 		string floatMsg;
    234 		string stringMsg;
    235 		
    236 		string stripCommas (string input);
    237 		bool testIntInput (string, string&, int l=-1, int u=-1);
    238 		bool testDoubleInput (string);
    239 		bool testCharInput (char, string);
    240 		bool testBoolInput (string);
    241 		bool getBoolValue (string);
    242 		void toUpperCase (string&);
    243 		bool ignoreCaseCompare (string, string);
    244 };