Snippet
/*
Project Software Engineer
Mr Gregory Hicks BIT
Sydney Australia
(C) 2006
This code may be used provided
credit is given for the source
*/
#include <cstdlib>
#include <iostream>
using namespace std;
int getUserInput ( const char* msg , const int min , const int max , const int quit ) ;
char getUserInput ( const char* msg , const char min , const char max , const char quit ) ;
// The alternative way is with a template. We loose the return type and replace
// that with a pointer to the value to modify
//template < class T >
//void getUserInput ( const char* msg , const T min , const T max , const T quit , T * value ) ;
//{
// T input ;
//
// do {
// cout << msg << " (Range: " << min << " to " << max << " , quit= " << quit << "): " ;
// cin >> input ;
// } while ( input != quit && ( input < min || input > max ) ) ;
// value = input ;
//}
int main(int argc, char *argv[])
{
int t = getUserInput ( "Enter an integer" , 0 , 10 , -1 ) ;
if ( t != -1 )
cout << " Integer entered is: " << t << endl ;
char c = getUserInput ( "Enter a letter" , 'A' , 'z' , '0' ) ;
if ( c!= '0' )
cout << "Letter entered is: " << c << endl ;
system("PAUSE");
return EXIT_SUCCESS;
}
/*
Purpose: To get an integer input from the user
Accepts: msg - display message
Min - smallest acceptable value
Max - largest acceptable value
quit - value to detect user wants to quit
Returns: users input
*/
int getUserInput ( const char* msg , const int min , const int max , const int quit )
{
int input ;
do {
cout << msg << " (Range: " << min << " to " << max << " , quit= " << quit << "): " ;
cin >> input ;
} while ( input != quit && ( input < min || input > max ) ) ;
return input ;
}
/*
Purpose: To get an char input from the user
Accepts: msg - display message
Min - smallest acceptable value
Max - largest acceptable value
quit - value to detect user wants to quit
Return: user entry
*/
char getUserInput ( const char* msg , const char min , const char max , const char quit )
{
char input ;
do {
cout << msg << " (Range: " << min << " to " << max << " , quit= " << quit << "): " ;
cin >> input ;
} while ( input != quit && ( input < min || input > max ) ) ;
return input ;
}
Copy & Paste
|