Like Tree3Likes
  • 1 Post By laserlight
  • 1 Post By GReaper
  • 1 Post By Elysia

String problem

This is a discussion on String problem within the C++ Programming forums, part of the General Programming Boards category; Correct me if I'm wrong, shouldn't this code pause for user input at cin.getline()? Seems like it's skipping for me. ...

  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
    22,265
    Yes, but perhaps there is a newline left in the input buffer, so the cin.getline consumes that line.
    striderx240 likes this.
    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

  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
    1,625
    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');
    striderx240 likes this.
    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
    Posts
    23,001
    >> 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);
    Elkvis likes this.
    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, 06:25 AM
  2. Replies: 22
    Last Post: 07-28-2011, 02: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, 11:17 AM

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