![]() |
| | #1 |
| Registered User Join Date: Mar 2003
Posts: 4
| getline run-on 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;
}
// 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 | |
| | #2 |
| Skunkmeister 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 | |
| | #3 |
| Registered User Join Date: Mar 2003
Posts: 4
| Thanks i'll give it a go . . . |
| twiz is offline | |
| | #4 |
| End Of Line Join Date: Apr 2002
Posts: 6,240
|
__________________ When all else fails, read the instructions. If you're posting code, use code tags: [code] /* insert code here */ [/code] |
| Hammer is offline | |
| | #5 |
| Registered User Join Date: Mar 2003
Posts: 4
| 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |