Parsing Inputs

This is a discussion on Parsing Inputs within the C++ Programming forums, part of the General Programming Boards category; Hi there! I am using the Jumping into C++ book as a guide to start writing C++. In the first ...

  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Like this:
    Code:
    if(!cin)
        cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(),'\n'); //#include <limits> for this
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by aaleclaire View Post
    What does the if (!cin) check for?
    Whether the input stream is in an error state.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    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
    21,650
    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.
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,650
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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


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