Skip to main content
gamedev.net
🔒 Locked

Problems with cin.getline

Started by Ivko 4 replies 650+ views
Original Post
Ivko
Ivko
My call to cin.getline isn't doing what it's supposed to. It immidiatly returns, without waiting for input. Here's a code snippet:
    
	char confirm[5];
	char newtitle[100];

	cout << "Current Title: " << CFGFile.title << "\n";
	cout << "Do you want to change it (y/n)? " << flush;
	cin.getline(confirm, 4);

	if(strcmp(confirm, "y") == 0) {
		cout << "Change to: ";

		cin.getline(newtitle, 100, '\n');
		if(strlen(newtitle) >= 100) {
			cout << "Maximum title length is 100 characters.\n";
			DoTitle();
		} else {
			//newtitle[strlen(newtitle)-1] = '\0';

			strcpy(CFGFile.title, newtitle);
		}
	}
     
When this function runs, it just shows the current title, display's do I want to change it, and returns. What am I doing wrong here? I don't see a reason for it to not work. Edited by - Ivko on November 29, 2001 10:14:54 PM
invective
invective
maybe you have an extra ''\n'' in the buffer. Call std::cin.ignore (200, ''\n''). Eats 200 characters or one ''\n''.
Ivko
Ivko
If I add that at the beggining of the function, all that happens is it hangs waiting for input before it prints the title (at the ignore statement). I was also thinking there might be characters left over in the input buffer. How would I clear the input buffer? Or is that the problem? I've tried cin.clear() and cin.delbuf(). BTW, I'm using VC++6.

Edited by - Ivko on November 29, 2001 10:20:43 PM
Ivko
Ivko
Still no luck. Am I providing too little information or is there no obvious reason why it''s happening? I can show some more code (the calling function) if you need.
donbonifacio
donbonifacio
well, I don''t know why, but on my lattest c++ homework, my prof created a getline function and as a comment, he said that he had created that because the VC++ getline had some bug...

try creating yourself that funcion and see if it still dont work.
Kylotan
Kylotan
Your ''prof'' should try looking into the problem instead of working around it. Yes, there is a problem with getline, but it''s easy enough to fix, and it shouldn''t cause the problem you have there.

Fixes for Library Bugs in VC++ V5.0/V6.0

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.