Thread: input data type

  1. #1
    Registered User QuestionKing's Avatar
    Join Date
    Jan 2009
    Posts
    68

    input data type

    I am trying to verify user input using <cctype>'s isdigit function. As I understand it's use, you pass a char and expect 0 or a non 0 (false/true) return.
    Assume the user enters more than one character. I am looking for the best way to ignore everything after the first char. I have been reading about clear and flush and on and on. I am trying to figure out what, if any, is the best way to handle ...for lack of better term... dumb input from the keyboard... There seem to be so many opinions and suggestions, I would like some clarity, perhaps a comparison of different ways to handle input? or maybe a term to google...
    thanks : )
    Asking a question you already know the answer to might teach you something you did not know...

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    There are two god ways. First, you can use getline to read a line of input into a string, and then ignore the rest of the string.

    The second is to read into a char variable using cin >> and then call cin.ignore(<some big number>, '\n'). The <some big number> can be 100 or 1000 or something, but then it won't work if they type 101 or 1001 characters. It can also be std::numeric_limits<std::streamsize>::max(), but then you have to #include <limits> and I think maybe <ios>, and some people don't like to use things they've never heard of before.

    If you're sure you only want a single digit (0-9), then either option is good. If you want to accept larger or negative numbers, then you'd have to adjust either one you use.

    If using getline, then use a stringstream to grab the integer from the string you read in. If using cin >>, then read into an int variable, but do it in a loop like this:
    Code:
    while (!(cin >> my_int_variable))
    {
      std::clear();
      std::ignore(1000, '\n');
      // prompt again here if you want.
    }

  3. #3
    Registered User QuestionKing's Avatar
    Join Date
    Jan 2009
    Posts
    68
    Ty for the info.
    I am working on a tic-tac-toe game, 1-9 to play the board, 0 to quit. So for that it should never need more options.
    It would seem for me reading a char would be best because I immediately isdigit(input). As far as numeric_limits, I think that a better way than just guessing at how long the cat may stand on the keyboard before stepping on enter. Will soon see how it works out for me, thanks.
    Asking a question you already know the answer to might teach you something you did not know...

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by QuestionKing View Post
    As far as numeric_limits, I think that a better way than just guessing at how long the cat may stand on the keyboard before stepping on enter.
    Yes, it returns the biggest possible number an int can hold, and since ignore accepts an int, it makes sure you can skip as much of the buffer as possible.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  4. Erros in Utility Header File
    By silk.odyssey in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2003, 06:17 AM
  5. Replies: 2
    Last Post: 05-12-2003, 04:40 PM