C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-02-2003, 05:25 AM   #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.
twiz is offline   Reply With Quote
Old 03-02-2003, 08:11 AM   #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
Stoned_Coder is offline   Reply With Quote
Old 03-02-2003, 06:25 PM   #3
Registered User
 
Join Date: Mar 2003
Posts: 4
Thanks i'll give it a go . . .
twiz is offline   Reply With Quote
Old 03-02-2003, 06:51 PM   #4
End Of Line
 
Hammer's Avatar
 
Join Date: Apr 2002
Posts: 6,240
http://faq.cprogramming.com/cgi-bin/...&id=1043284392
__________________
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
Hammer is offline   Reply With Quote
Old 03-02-2003, 06:59 PM   #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.
twiz is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re-doing a C program to run in Win2000 or XP fifi C Programming 5 08-17-2007 05:32 PM
how to run an exe command in c++ and get back the results? mitilkhatoon C++ Programming 5 09-21-2006 06:00 PM
calculating the mode bigggame C Programming 10 06-13-2006 03:04 AM
How I can Run exe file in C++ palang C++ Programming 2 05-10-2006 11:55 AM
EXE Closing all instances of Internet explorer - How can it run in Win 95/98 joh Windows Programming 2 10-29-2002 04:56 PM


All times are GMT -6. The time now is 04:11 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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