String input looping my hash function

This is a discussion on String input looping my hash function within the C++ Programming forums, part of the General Programming Boards category; Hi, ok ive had to make a prog for my uni and i have it all working perfectly apart from ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    14

    String input looping my hash function

    Hi, ok ive had to make a prog for my uni and i have it all working perfectly apart from one small detail. To search my hash fuction it takes in an int value and searches the table. If i input a string value the output loops.

    Ideally i would want to be able to out put something long the lines of "Invalid input, please enter a value between 10-100000"

    Any help would be great thanks.......

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Ideally i would want to be able to out put something long the lines of "Invalid input, please enter a value between 10-100000"
    Code:
    std::cout << "Invalid input, please enter a value between 10-100000";
    So what's the problem?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Example:
    Code:
    int x;
    
    std::cout << "Please input a value between 10 and 100,000" << std::endl;
    std::cin >> x;
    
    if(x < 10 || x > 100000)
      std::cout << "Silly rabbit, that isn't between 10 and 100,000" << std::endl;

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    On second thought... I think you are asking how to construct a loop to resolve this issue, correct?

    Example:
    Code:
    int getInput()
    {
       bool valid = false;
       int input;
    
       do
       {
          std::cout << "Please input a hash value (10 - 100,000)" << std:: endl;
          std::cin >> input;
    
          if(input >= 10 && input <= 100000)
            valid = true;
       } while(!valid);
    
       return input;
    }

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    ah a do while with a bool, yeah should be perfect i'll give it a bash tomorrow, i know its silly but the rest works perfectly want it all working spot on

    thanks

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    No problem, though its good practice to tell a user what they did wrong (which I neglected in my second code). Good luck.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >If i input a string value the output loops.
    Then you can either read the input into a std::string, and afterwards convert it to an int, or you can try reading the input into an int, verify the input stream is still good, and if it's not, clear the input stream (cin.clear()), ignore what's left in the stream (using cin.ignore), and try again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21