Thread: getline run-on

  1. #1
    Registered User
    Join Date
    Mar 2003

    getline run-on

    Hi all,
    I have checked in archives but can't find any helpful posts.

    It seems that I can't stop any program when the number of maximum characters in getline is exceeded. Here is a test program . . .

    #include <iostream.h>
    int main()
         char name[4];                   // declare cstring 3chars long + null
         cout << "\nType now :";
         cin.getline(name, 4);           // this should truncate to 3 chars 
                                                   // then discard the rest.
         cout << "You typed :" << name;
         int wait;
         cin >> wait;            // should wait for int input 
                                        // irrespective of whether name > 3 or not
         return 0;
    // THE PROBLEM: it just won't wait. If the number of characters input exceeds 3

    // then program terminates while in other programs, the endless loop appears!

    // SOLUTION: ???

    It appears that \n stuck in the input stream, the above program just terminates and others endless loop as if repeated \n, no matter how many iterations of cin >> wait of cin.ignore().

    I have tried everything I can think of including another cin.getline command, but can’t stop it if 4 characters (in this case) are input.

    Additionally, I have tested and the variable ‘name’ does truncate fine to 3 characters (in this case).

    Am I suffering from tunnel vision today and doing some thing fundamentally wrong?

    Thanks, any help appreciated.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001


    before the cin>> and after the cin.getline()
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :-

  3. #3
    Registered User
    Join Date
    Mar 2003
    Thanks i'll give it a go . . .

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Mar 2003


    Thanks Hammer,

    Had seen that info but will go through it again.

    I was assuming the getline would not only discard the data after max characters reached as it says it will, but would also clear the instream buffer.

    I was hoping for a simple solution.

    Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Re-doing a C program to run in Win2000 or XP
    By fifi in forum C Programming
    Replies: 5
    Last Post: 08-17-2007, 05:32 PM
  2. how to run an exe command in c++ and get back the results?
    By mitilkhatoon in forum C++ Programming
    Replies: 5
    Last Post: 09-21-2006, 06:00 PM
  3. calculating the mode
    By bigggame in forum C Programming
    Replies: 10
    Last Post: 06-13-2006, 03:04 AM
  4. How I can Run exe file in C++
    By palang in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2006, 11:55 AM
  5. Replies: 2
    Last Post: 10-29-2002, 04:56 PM