Thread: How do I allow spaces on a cin?

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    8

    Question How do I allow spaces on a cin?

    So I am creating this code on Dev C++ to cin a information about an employee that will later be output to a text file. It includes Name, ID number, Phone Number, Address, etc.

    Address will obviously take two to three spaces to be input correctly. EX: 1337 Computer Ln.

    How can I allow it to include the spaces?

    Do I use getline?

    If so, how?


    Thanks in advance!

  2. #2

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Note that if you use cin >> to read in the ID number or any other entry, and also use getline, you will get strange results. Mixing the two forms of input can cause problems because cin >> leaves the newline (from when the user hits <enter>) in the input stream and getline reads that and stops immediately.

    One solution is to always call cin.ignore() once right after any calls to cin >> (but never right after a call to getline).

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    8

    Question

    Quote Originally Posted by Daved View Post
    Note that if you use cin >> to read in the ID number or any other entry, and also use getline, you will get strange results. Mixing the two forms of input can cause problems because cin >> leaves the newline (from when the user hits <enter>) in the input stream and getline reads that and stops immediately.

    One solution is to always call cin.ignore() once right after any calls to cin >> (but never right after a call to getline).
    Could you give an example with my particular situation?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
      std::string name;
      std::cout << "What is your full name: ";
      std::getline(std::cin, name);
    
      int age = 0;
      std::cout << "How old are you: ";
      std::cin >> age;
      std::cin.ignore(); // ignore the newline here, after the call to cin >>.
    
      std::string address1;
      std::string address2;
      std::cout << "What is your address line 1: ";
      std::getline(std::cin, address1);
      std::cout << "What is your address line 2: ";
      std::getline(std::cin, address2);
    
      std::cout << '\n' << name << " is " << age << " years old and lives at:\n";
      std::cout << address1 << '\n' << address2 << '\n';
    }
    (I didn't compile or test that, but hopefully you get the point. The part in red is what I was talking about.)

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    8
    When I use getline the same exact way you are using it, it completely skips it like there isn't even a cin there. I saw on other places online they use it the same way too. It doesn't cause an error it just skips it.

    Is this a problem with dev-C++? Or maybe vista? I heard that vista screw up C++ but I have never ran into any problems until now of course.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That's the behavior that occurs if you're missing the line in red in my example. Did you add cin.ignore(); after every call to cin >>?

    There is a bug in an old version of Visual C++ that shows the same thing, but since you're using Dev-C++ it shouldn't be a problem.

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    8
    Quote Originally Posted by Daved View Post
    That's the behavior that occurs if you're missing the line in red in my example. Did you add cin.ignore(); after every call to cin >>?

    There is a bug in an old version of Visual C++ that shows the same thing, but since you're using Dev-C++ it shouldn't be a problem.

    I mean it skips the geline(cin, name) not the following cin.

    I used the cin.ignore()

    maybe I am missing something

    Code:
        
    
        cout<<"Please enter the following information:"<<endl;
        cout<<"Name: ";
        getline(cin, name);
        cout<<endl;
        cout<<"House Number: ";
        cin>>houseNumber;
        cin.ignore();
    When I run it, it skips name and goes right to houseNumber.

    hmmm.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You probably have to add another cin.ignore() somewhere before this code snippet.
    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
    Sep 2009
    Posts
    8
    Quote Originally Posted by laserlight View Post
    You probably have to add another cin.ignore() somewhere before this code snippet.
    That did it! Thank you so much!

    It's strange, I ran his code in a different project and it worked fine the way he had it. But in my code I have to put the ignore before every chunk of getlines that I use and I don't have to put one after them.

    O.o

    I'm so confused. But at least it works I guess... Any explanations for this? haha

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    My original advice was to put cin.ignore() after all calls to cin >>. That's different than putting it before calls to getline. Make sure you follow the original advice and you won't have any troubles.

    The reason it matters is because the ignore is needed for something that happens with the cin >> call. When the user types something and hits <enter>, the text is added to the cin buffer and a newline character for the <enter> is added as well. But cin >> reads until it hits any whitespace (space, tab or newline) and stops. So it read the input and stops at the newline. That newline is left in the buffer.

    If your next call is another cin >>, then the newline that is sitting there is ignored, because operator>> ignores any whitespace before actual characters. So that's why you don't have this problem if you only use cin >>.

    However, getline doesn't ignore whitespace. It reads until it finds a newline, even if the first character is a newline. So that leftover newline from the cin >> call is the first character in the buffer and the getline function sees it and stops. This causes your problem.

    So why not put the cin.ignore() before calls to getline? Well, because you'd have problems if you had two calls to getline back to back. That's because the getline function automatically removes the newline from the buffer. So if you called ignore() before getline, but the previous read from the buffer also used getline, then the newline wouldn't be there and you'd be ignoring real data.

    So the moral is that adding cin.ignore() after all calls to cin >> should solve the problem and not cause any other problems anywhere else. Adding cin.ignore() before all calls to getline could cause other errors and should be avoided.

    If it seems annoying that these two things don't work well together, it is. But they were designed for two different methods of reading input. Following the simple rule I've outlined helps a lot when working with them together.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ASCII File Copy Problem: Expand tabs to multiple spaces
    By matrixx333 in forum C Programming
    Replies: 5
    Last Post: 10-21-2009, 03:13 AM
  2. Changing 3 spaces to tabs
    By dnguyen1022 in forum C Programming
    Replies: 2
    Last Post: 12-22-2008, 12:51 AM
  3. Tabs or Spaces
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 46
    Last Post: 04-08-2007, 11:45 AM
  4. getline(function)??
    By dac in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2006, 12:42 PM
  5. Handling spaces in command line arguments
    By starkhorn in forum C++ Programming
    Replies: 7
    Last Post: 11-16-2004, 02:42 PM

Tags for this Thread