getline run-on

This is a discussion on getline run-on within the C++ Programming forums, part of the General Programming Boards category; Hi all, I have checked in archives but can't find any helpful posts. It seems that I can't stop any ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    4

    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 . . .

    Code:
     
    
    #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
    Posts
    2,572
    add....

    cin.ignore(80,'\n');

    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 :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

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

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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
    Posts
    4

    Smile

    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, 06: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, 07:00 PM
  3. calculating the mode
    By bigggame in forum C Programming
    Replies: 10
    Last Post: 06-13-2006, 04:04 AM
  4. How I can Run exe file in C++
    By palang in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2006, 12:55 PM
  5. Replies: 2
    Last Post: 10-29-2002, 04:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21