Thread: How to see if a user inputed a string

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    6

    How to see if a user inputed a string

    Hello everyone,

    I trying to make a game in C++ right now and I want to make it so when a user types in
    a String instead of a int the whole program won't go crazy.
    And I have no idea how to do that so if you know how please post.


    Thanks.
    ~herocks

  2. #2
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    You could read it into a string, then use a function to extract the number from the string. If there is no number in the string then tell the player they must enter a number. Look up std::stringstream.

    Good Luck.
    David

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    6
    Ah, I was thinking of doing that, but I only thought of half of what I should have thought.
    Thanks, I'll try it now.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    6
    Ok, Now I needa know how to convert a string to a int...I tried using the atoi() function but all it got me was a 0...

  5. #5
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    You can use stringstream for that.

    something like this:
    Code:
    std::string str;
    int an_int;
    std::stringstream s;
    s<< str;
    s>> an_int;

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    6
    Thanks, but I just got it to work.
    Atoi returned a 0 ( NULL ) because I typed in a letter so I just have to see if the int equals NULL
    and if it dose, tell the user that its invalided.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    IMO, the easiest way to check if the user inputs a string instead of an int is to check the state of the stream when you read in. When the user inputs a string and you attempt to read in an int, it will go into a fail state. This example clears the fail state, empties the stream of the invalid input, and attempts to read in the int again:
    Code:
    int value = 0;
    while (!(std::cin >> value))
    {
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        // Error message and prompt again here.
    }
    You have to #include <limits> and <ios> for numeric_limits and streamsize. You can expand that pretty easily to also "fail" if the user enters an integer followed by characters, like 123abc.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    What about:
    Code:
    cin.ignore(cin.rdbuf()->in_avail() );
    vs.
    Code:
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In my experience both work (don't forget the '\n' on the first one), but I believe the standard actually uses the numeric_limits version as an example so I prefer that one. I haven't read enough about in_avail() to be completely confident of its accuracy.

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM