1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
#include<string>
#include<iostream>
#include"Tools.h"
#include"CompileSwitches.h"
using namespace std;
namespace tools_namespace
{
const int SCREEN_WIDTH = 80;
string swab( char swabValue, int howMany )
{
string swabString;
for ( int charsNeeded = howMany
; charsNeeded > 0
; --charsNeeded )
swabString = swabString + swabValue;
return swabString;
}
void pause( string prompt )
{
cout << prompt;
cin.ignore(999,'\n');
}
// clear input garbage
void flush( void )
{
cin.ignore(999,'\n');
}
int getInt( string prompt )
{
int userInput;
while (true)
{
cout << prompt;
cin >> userInput;
flush();
if ( !cin.fail() ) break;
handleInputError("Non-Numeric input.");
}
return userInput;
}
float getFloat( string prompt )
{
float userInput;
while (true)
{
cout << prompt;
cin >> userInput;
flush();
if ( !cin.fail() ) break;
handleInputError("Non-Numeric input.");
}
return userInput;
}
int magnitude ( int value )
{
return (value>=0) ? value : -value;
}
int width( int number )
{
int digitCount = 0;
if ( number < 0 )
{
++digitCount; // for the sign
number = -number;
}
for ( ; number >= 10 ; number /= 10 )
++digitCount;
++digitCount; // for the last digit
return digitCount;
}
int minimum( int a, int b )
{
return ( a < b ) ? a : b;
//if ( a < b )
// return a;
//else
// return b;
}
int maximum( int a, int b )
{
return ( a > b ) ? a : b;
//if ( a < b )
// return a;
//else
// return b;
}
int getBoundedInt( string prompt,
int lowerBound, int higherBound )
{
int userInput;
while (true)
{
userInput = getInt( prompt );
if ( userInput >= lowerBound
&& userInput <= higherBound )
break;
cout << "Value must be in the range "
<< lowerBound
<< "..."
<< higherBound
<< ". Try again."
<< endl;
}
return userInput;
}
int getPositiveInt( string prompt )
{
return getBoundedInt( prompt, 1, INT_MAX );
}
int getNonNegativeInt( string prompt )
{
return getBoundedInt( prompt, 0, INT_MAX );
}
char getChar( string prompt )
{
char userInput;
while (true)
{
cout << prompt;
cin >> userInput;
flush();
if ( !cin.fail() ) break;
handleInputError("Input failed.");
}
return userInput;
}
string getString( string prompt )
{
string userInput;
while (true)
{
cout << prompt;
cin >> userInput;
flush();
if ( !cin.fail() ) break;
handleInputError("Input failed.");
}
return userInput;
}
void handleInputError( string message )
{
cin.clear();
flush();
cout << message << " Try again." << endl;
}
string getLine( string prompt )
{
string userInput;
while (true)
{
cout << prompt;
getline(cin,userInput);
if ( !cin.fail() ) break;
cin.clear();
flush();
cout << "Input failed - Try again." << endl;
}
return userInput;
}
bool getBool( string prompt )
{
while (true)
{
char userInput = getChar(prompt);
if ( userInput == 'y' || userInput == 'Y' )
return true;
if ( userInput == 'n' || userInput == 'N' )
return false;
cout << "Please enter y or n." << endl;
}
}
bool isOdd( int aNumber )
{
#ifndef GROSS
return magnitude(aNumber)%2 == 1;
#else
if ( aNumber < 0 )
aNumber = -aNumber;
int remainder = aNumber%2;
if ( remainder == 1 || remainder == -1)
return true;
else if ( remainder != 1 )
return false;
#endif
}
}
|