Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 95,549 C++ Programmers for FREE!. Ask your question and get quick answers from Dream.In.Code experts. There are 926 online right now! We're the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a C++ Expert

Register to Make This Box Go Away!



Functional Decomposition - Getting Ranged User Input

Sample shows practical way to get user input using functional decomp to create range testing and quit values into a well designed code block. Uses overlaoded versions to get input in different types.

Submitted By: gregoryH
Actions:
Rating:
Views: 1,938

Language: C++

Last Modified: October 16, 2006
Instructions: copy and paste this functional code and try it for yourself.

Many new people write lots of code never thinking to boil things down to smaller more readable and maintainable blocks.

Snippet


  1. /*
  2.    Project Software Engineer
  3.       Mr Gregory Hicks BIT
  4.         Sydney Australia
  5.            (C) 2006
  6.        
  7.    This code may be used provided
  8.    credit is given for the source
  9. */
  10.  
  11. #include <cstdlib>
  12. #include <iostream>
  13.  
  14. using namespace std;
  15.  
  16. int  getUserInput ( const char* msg , const int  min , const int  max , const int  quit ) ;
  17. char getUserInput ( const char* msg , const char min , const char max , const char quit ) ;
  18.  
  19. // The alternative way is with a template. We loose the return type and replace
  20. // that with a pointer to the value to modify
  21. //template < class T >
  22. //void  getUserInput ( const char* msg , const T min , const T max , const T quit , T * value ) ;
  23. //{
  24. //    T input ;
  25. //   
  26. //    do {
  27. //        cout << msg << " (Range: " << min << " to " << max << " , quit= " << quit << "): " ;
  28. //        cin >> input ;
  29. //    } while ( input != quit && ( input < min || input > max ) ) ;
  30. //    value = input ;
  31. //}
  32.  
  33. int main(int argc, char *argv[])
  34. {
  35.     int t =  getUserInput ( "Enter an integer" , 0 , 10 , -1 ) ;
  36.     if ( t != -1 )
  37.       cout << " Integer entered is: " << t << endl ;
  38.      
  39.     char c = getUserInput ( "Enter a letter" , 'A' , 'z' , '0' ) ;
  40.     if ( c!= '0' )
  41.       cout << "Letter entered is: " << c << endl ;
  42.      
  43.     system("PAUSE");
  44.     return EXIT_SUCCESS;
  45. }
  46.  
  47. /*
  48.    Purpose: To get an integer input from the user
  49.    Accepts: msg - display message
  50.             Min - smallest acceptable value
  51.             Max - largest acceptable value
  52.             quit - value to detect user wants to quit
  53.    Returns: users input
  54. */
  55. int getUserInput (  const char* msg , const int  min , const int  max , const int quit  )
  56. {
  57.     int input ;
  58.    
  59.     do {
  60.         cout << msg << " (Range: " << min << " to " << max << " , quit= " << quit << "): " ;
  61.         cin >> input ;
  62.     } while ( input != quit && ( input < min || input > max ) ) ;
  63.     return input ;
  64. }
  65.  
  66. /*
  67.    Purpose: To get an char input from the user
  68.    Accepts: msg - display message
  69.             Min - smallest acceptable value
  70.             Max - largest acceptable value
  71.             quit - value to detect user wants to quit
  72.    Return: user entry
  73. */
  74. char getUserInput ( const char* msg , const char min , const char max , const char quit )
  75. {
  76.     char input ;
  77.    
  78.     do {
  79.         cout << msg << " (Range: " << min << " to " << max << " , quit= " << quit << "): " ;
  80.         cin >> input ;
  81.     } while ( input != quit && ( input < min || input > max ) ) ;
  82.     return input ;
  83. }
  84.  
  85.  

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month
-->