Thread: Getline messing up code

  1. #16
    Registered User
    Join Date
    Jan 2014
    Posts
    17
    Thanks Elysia, that's the habit I'm going with too.

    If I can indulge one more question about getline and input buffers.

    Code:
    #include <iostream>
    #include <string>
    
    
    
    
    int main()
    
    
    {
        std::string user_last_name;
        std::string left_over;
        
        std::cout << "Enter name [format: last, first]:\n";
        
        getline( std::cin, user_last_name, ',' );
        
        std::cout << user_last_name << "\n";
        std::cin >> left_over;
        std::cout << left_over << "\n";
        
        return 0;
    }
    I've set getline to define user_last_name up till ",". Now if I were to type "Doe, John M" it correctly spits out:
    Doe
    John

    What happens to 'M'? Shouldn't it be part of the left over in the input buffer? Can input leftover like that throw a wrench into coding when new strings are introduced?

  2. #17
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    What happens to 'M'? Shouldn't it be part of the left over in the input buffer?
    Yes, and it is.

    cin >> left_over; is behaving correctly, because by default the operator stops reading when it encounters white space.

    Can input leftover like that throw a wrench into coding when new strings are introduced?
    Yes. If you tried to read another string, " M" would be part of it.

  3. #18
    Registered User
    Join Date
    Jan 2014
    Posts
    17
    For veterans of C++, is getline your preferred way of getting user input? What if you're dealing with both numbers and strings? Storing a number in a string would make computations complicated. Finally, if you're going to be using getline and risk stuff left in the buffer, is there a function that you can easily put in code to auto clear it as you go?

  4. #19
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    For veterans of C++, is getline your preferred way of getting user input?
    Having a preference can be troubling; there are times when you'll be working a square peg into a round hole because you want the program to work according to said preference. Since cin is line buffered though, using std::getline makes a lot of sense, most of the time.

    ? What if you're dealing with both numbers and strings?
    Convert where necessary.

    Finally, if you're going to be using getline and risk stuff left in the buffer, is there a function that you can easily put in code to auto clear it as you go?
    std::getline will read entire lines if you do not change the delimiter from the default one, which is \n or EOF or something like that, so there is no left over input to be read. It's when you change the delimiter that you need to be more careful. In particular, if the input is not good, then you will have problems arising from those errors.

    One way to do what you want is to validate the string before you split it up. This means that the input device doesn't need to actually split it, and there will be less errors related to that from the input device.

    Code:
    #include <string>
    #include <sstream>
    #include <iostream>
    
    bool isformatted(const std::string& input);
    
    int main() {
    	std::string last, first, middle;
    	std::string name;
    	do {
    		std::cout << "Enter your name last, first middle:\n";
    		getline(std::cin, name);
    	} while ( !isformatted(name) );
    	std::istringstream splitter(name);
    	std::getline(splitter, last, ',');
    	splitter >> first >> middle;
    	std::cout << "You entered: " << first << ' ' << middle << ' ' << last << std::endl;
    }
    
    bool isformatted(const std::string& input) {
    	return input.find(", ") != std::string::npos && input.find_last_of(' ') != std::string::npos;
    }
    
    #if 0
    my output
    
    Enter your name last, first middle:
    John Q. Public
    Enter your name last, first middle:
    Public, John Q.
    You entered: John Q. Public
    #endif
    Last edited by whiteflags; 01-03-2014 at 07:00 PM.

  5. #20
    Registered User
    Join Date
    Jan 2014
    Posts
    17
    Thanks for the example code. I'll have to study it. A few new concepts in there I haven't caught up to but that's fine, I get the general jist of it.

    What do you think about my example below? While avoiding using more advanced concepts, does it come across as forced and could be done a bit more elegantly?

    Code:
    #include <iostream>
    #include <sstream>
    
    
    int main(int argc, const char * argv[])
    {
    
    
        // insert code here...
        
        std::string input;
        int age1;
        int age2;
        std::stringstream stream;
        
        std::cout << "Hello, please enter the ages of two people" << std::endl;
        std::getline(std::cin, input, ' ');
        std::cin >> age2;
        
        stream << input;
        stream >> age1;
        
        std::cout << "You entered ages " << age1 << " & " << age2 << std::endl;
        
        
        return 0;
    }

  6. #21
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well if you were trying to emulate me, I don't see how what you wrote is different from:

    Code:
    std::getline( std::cin, somestring );
    std::stringstream stream;
    stream << somestring;
    stream >> age1 >> age2;
    somestring has all the input inside of it thanks to the way std::getline was called.

  7. #22
    Registered User
    Join Date
    Jan 2014
    Posts
    17
    Wow, thanks a lot. You're the man/woman! Didn't know you could treat the input stream like that.

  8. #23
    Registered User
    Join Date
    Jan 2014
    Posts
    1

    header file

    is the "string" header correct?
    shouldn't it be "cstring"?

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    std::string was used rather than functions that operate on null terminated C-style strings, hence <string>, not <cstring>, is correct.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-13-2011, 07:32 AM
  2. CWinThread messing itself up
    By LowlyIntern in forum C++ Programming
    Replies: 7
    Last Post: 01-16-2009, 08:08 AM
  3. Messing with folders
    By Probose in forum C++ Programming
    Replies: 2
    Last Post: 09-21-2006, 03:16 PM
  4. Is someone messing with m$ again?
    By exluddite in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 09-16-2004, 01:56 PM
  5. Do you think this might be messing up my brain?
    By incognito in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 01-21-2002, 08:33 AM