Thread: Parsing Inputs

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    8

    Question Parsing Inputs

    Hi there! I am using the Jumping into C++ book as a guide to start writing C++. In the first examples, we are asked to read in some numbers and print them out. If I set the variable type to read to an int, like this: cin >> my_int;

    But then when prompted to enter the input I type 2.2 (not an integer), then the rest of the line (the ".2") is stored in the input buffer and screws up later requests for data.

    Could someone please recommend a good way to safely get input from the command line, check it type correctness, and throw away extra data?

    thanks so much!
    Aaron

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Like this:
    Code:
    if(!cin)
        cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(),'\n'); //#include <limits> for this

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    8
    Quote Originally Posted by manasij7479 View Post
    Like this:
    Code:
    if(!cin)
        cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(),'\n'); //#include <limits> for this

    Thank you! What does the if (!cin) check for?
    Aaron

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by aaleclaire View Post
    What does the if (!cin) check for?
    Whether the input stream is in an error state.

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    8
    Ah, okay. I will study the input error states some more later. Now, the cin.ignore will ignore and throw away the fraction part of a number if the cin is directed to a type int? I will try it later today (once I get done with my "real" job ).

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, it will "flush" the input buffer.
    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.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by aaleclaire
    Could someone please recommend a good way to safely get input from the command line, check it type correctness, and throw away extra data?
    The way I recommend is to read the line into a string, then parse the string to extract the specific input. This way, you don't need to "throw away extra data" because the next read will read the next line, rather than whatever is left on the line. This also avoids the problem where you read into an int, then try to read the next line into a string, only to find that you read nothing into the string because you ended up reading the rest of the previous line instead of the next line.

    Unfortunately, doing this, while simple, requires more stuff that you probably don't know, unlike manasij7479's suggestion, e.g.,
    Code:
    // Headers to include
    #include <iostream>
    #include <string>
    #include <sstream>
    
    // Read a line from standard input.
    std::string line;
    std::getline(std::cin, line);
    // Create an input string stream from the line to extract the int
    std::istringstream ss(line);
    int my_int;
    if ((ss >> my_int) && ss.eof())
    {
        // Use my_int
    }
    else
    {
        // Could not extract the int, or there was invalid input after extraction.
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jun 2012
    Posts
    8
    Thank you laserlight! I am using getline() to read in the command line as you suggest. The part that I have not seen before is istringstream, but I get the idea. Is there a generic way to read in the command line (user data) and parse the various strings separated by some delimiter into a vector? For example, if the input were 2 2.2 abc, then the vector vec[0] = 2, vec[1] = 2.2, and vec[2] = "abc". That way the length of the vector would tell me if I got the right number of parameters. Then I could use your recommendation to check the types of each of the parameters. I am also open to other suggestions.
    Thanks! aaleclaire.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You could use the delimiter of getline, or in this case as your tokens are separated by whitespace, use operator>> to read into a std::string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jun 2012
    Posts
    8
    Thanks again. I will give it a try. This is my second day learning C++, so I appreciate the help! I am a hardware guy who needs a change!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loop, Inputs
    By meandmyipod in forum C Programming
    Replies: 3
    Last Post: 09-24-2011, 05:49 AM
  2. How to handle inputs if the user inputs the wrong thing
    By bassist11 in forum C Programming
    Replies: 5
    Last Post: 09-22-2010, 04:28 AM
  3. Help with inputs
    By RustySpoon in forum C Programming
    Replies: 3
    Last Post: 03-14-2010, 09:38 PM
  4. Keyboard Inputs
    By andreas_himself in forum C Programming
    Replies: 17
    Last Post: 08-15-2008, 10:35 PM
  5. String parsing(parsing comments out of HTML file)
    By slcjoey in forum C# Programming
    Replies: 0
    Last Post: 07-29-2006, 08:28 PM

Tags for this Thread