View Full Version : Pointer and Cin Problem

Apr 9, 2007, 11:12 PM
Below is what I use in Visual studio. Using getline I can have names entered that have spaces etc. If there is a space and I use cin, I get an infinite loop. (other code doesn't matter, it's just a function call.

All variables have been declared ok. What do I use insteasd of clear buffer etc on OS X?

void GetString(char* str) {

cout << "Please Enter a String: " << endl;
cin >> str;
// cin.ignore(cin.rdbuf()->in_avail());
//cin.getline(str, 100);
// cin.clear();
//cin.ignore(cin.rdbuf ()->in_avail());

cout << endl;


Apr 9, 2007, 11:33 PM
Not sure I understand exactly, but getline works fine for reading spaces:

void GetString(char* str)
cout << "Please Enter a String: " << endl;
cin.getline(str, 100);
cout << endl;

Apr 9, 2007, 11:36 PM
But when I try to run that function, it skips completely over my whole function call. The line "Enter your string" is printed, but I am never prompted to enter anything. It skips right along to printing the menu again. :(

Apr 10, 2007, 07:08 AM
Try clearing the input buffer before you "cout".


Apr 10, 2007, 03:52 PM
Hmmm... If I remember correctly the input needs to be put to a variable.

Just using cin or it's subfunctions doesn't do anything because it has nowhere to put it. Or at least that's what I'm getting from the code.


Apr 10, 2007, 04:43 PM

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');