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.