Thread: I/O checking. "IntegerInputHandler"

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    86

    I/O checking. "IntegerInputHandler"

    Ok, i am building an application, that will take multiple numeric input from an user, as in "choises", where we got a maximum number representing the boarder of numbers u can pick. i have made this function for it:

    Code:
    int IntegerInputHandler ( int lengths [], int index )
    {
      int maxchoise = lengths[index];
      int choise = 0;
      std::string Handler;
      std::cout <<"InpuT:";
    
      std::getline (std::cin,Handler);
      std::stringstream (Handler) >> choise ;
    
      while (choise > maxchoise )
      {
        std::cerr <<"Bad Input, Retype";
        std::getline (std::cin,Handler);
        std::stringstream (Handler) >> choise ;
      }
      return (choise);
    }
    Code:
    int main ()
    {
      int lengths [] = {4,100000};
      int index = 2;
      IntegerInputHandler ( lengths,index );
      int choise = 0;
    
    }
    it takes 2 parameters, the first one is an array with all tha maximum numbers in ,and the second parameter is the index of the array where the number is we want. but now my problem, assume that index 2 is chosen as in my example, and the user enters A, as input this will validate cause it will be converted to the ANSCII number of the letter, right? how do i prevent this, how do i prevent that annything else being input besides an int gets rejected and u will be asked to give new input....

  2. #2
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    How about this?
    Code:
    int IntegerInputHandler( int lengths[], int index ) {
      int choice = 0;
    
      while ( !( std::cin >> choice ) || choice > lengths[index] ) {
        std::cin.clear();
        std::cin.ignore( 256, '\n' );
        std::cerr << "Bad Input, Retype> ";
      }
    
      return choice;
    }
    
    
    int main() {
      int lengths[] = {4,100000};
      int choice = IntegerInputHandler( lengths, 1);
    
      std::cout << choice << '\n';
    }
    I think it's better to pass the length instead of an array and an index. That way you can use the function even if you don't have an array, and the fewer parameters you have, the fewer parameters you have to remember.
    Code:
    int GetInteger( int max ) {
      int rv = 0;
    
      while ( !( std::cin >> rv ) || rv > max ) {
        std::cin.clear();
        std::cin.ignore( 256, '\n' );
        std::cerr << "Bad Input, Retype> ";
      }
    
      return rv;
    }
    
    
    int main() {
      int lengths[] = {4,100000};
      int choice = GetInteger( lengths[1] );
    
      std::cout << choice << '\n';
    }

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> int maxchoise = lengths[index];
    index is 2 but lengths only contains two entries so that index is out of bounds. Only indexes 0 and 1 are valid for an array of size 2.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    86
    Quote Originally Posted by Daved View Post
    >> int maxchoise = lengths[index];
    index is 2 but lengths only contains two entries so that index is out of bounds. Only indexes 0 and 1 are valid for an array of size 2.
    yea, im sorry i was just doing somthing fast.... didnt compile yet.

    and tnx noir, but when i type in 1d => it also passes the loop , and that was my problem all along actualy lol :P
    single characters represent integers too... so they will pass
    Last edited by epidemic; 04-03-2007 at 03:01 PM. Reason: and tnx noir

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  3. why page based I/O can improve performance?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 06-12-2006, 07:42 AM
  4. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM
  5. Overlapped I/O and Completion Port :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 10-30-2002, 05:14 PM