Thread: String problem

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    13

    Question String problem

    Correct me if I'm wrong, shouldn't this code pause for user input at cin.getline()? Seems like it's skipping for me.

    Code:
    void GetName(){    
        char Name[50];
        cout<<"Please Enter your name: ";
        cin.getline(Name,50);
        cout<<Name<<", is this correct?\n";
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, but perhaps there is a newline left in the input buffer, so the cin.getline consumes that line.
    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

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    13
    Quote Originally Posted by laserlight View Post
    Yes, but perhaps there is a newline left in the input buffer, so the cin.getline consumes that line.
    If this is true, how would I remedy this?

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    110
    try and flush it.
    Code:
    flush(cout);

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by überfuzz View Post
    try and flush it.
    Code:
    flush(cout);
    "flush(cout)" flushes the output buffer!

    The only way to "flush" standard input is:
    Code:
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    13
    Thanks, got it working now!

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >> char Name[50];
    >> cout<<"Please Enter your name: ";
    >> cin.getline(Name,50);
    Should be

    std::string Name;
    std::cout << "Please Enter your name: ";
    std::getline(Name, std::cin);
    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. Replies: 5
    Last Post: 08-10-2011, 05:25 AM
  2. Replies: 22
    Last Post: 07-28-2011, 01:26 PM
  3. C String Problem: Not reading end of string
    By sedavis4 in forum C Programming
    Replies: 5
    Last Post: 11-17-2008, 10:29 PM
  4. Replies: 0
    Last Post: 04-05-2003, 09:33 AM
  5. Problem comparing string from text file with string constant
    By XenoCodex Admin in forum C++ Programming
    Replies: 3
    Last Post: 07-25-2002, 10:17 AM

Tags for this Thread