hanging newline

Started by
4 comments, last by conflict 20 years ago
oki, i am using char* strings, and i am reading in using cin.get(buf, 52); in a while game loop.. this work fine, until i input only a carried return/newline then the game loops in and out of my get_input function in a never ending loop O_o.. i have tried, cin.ignore, fflush, even a "dummy" cin.get(...) to catch it but these don't work. this is an example of what my code input code:

int main()
{
	char *str = new char[11];

	do
	{

		std::cout << "Enter text: ";
		std::cin.get(str,11);

	}while(strcmp(str, "quit") !=0);
	
	delete[] str;
	return 0;
}

  
please help :o thanks Lee [edited by - conflict on April 4, 2004 2:46:26 PM]
|:Lee
Advertisement
string input;
while (getline(cin, input) && input != "quit")
{
// do whatever with input here
}
OMG, hehe 10mins after posting i resolve it! \o/

cin.clear(); // because cin.get.. fails on CR \ \n.. cin state stays failed... thus b0rks, need reseting.
fflush(stdin); //flush hangline newlines.

love it

\o/

bah.. me thinks i gave up too soon

thanks
Lee

[edited by - conflict on April 4, 2004 3:07:38 PM]

[edited by - conflict on April 4, 2004 3:08:23 PM]
|:Lee
:o

like i said, using char* (cstrings), which re vastly implemented throughout my game :o

tbh, i wish i had used std::string.. then i wouldn''t have to have sorted overflow issues...

but char * is good ptr practice

thanks
/me absorbs code :D
|:Lee
You know std::string has the member function c_str(), which returns a const char*? This way you can use strings in your new functions and still call the old ones.


---
Just trying to be helpful.

Sebastian Beschke
Just some student from Germany
http://mitglied.lycos.de/xplosiff
---Just trying to be helpful.Sebastian Beschkehttp://randomz.heim.at/
jea i know c_str, so it can be used with legacy funtions..

but like i said, i have implemented char* through out me game, not gunna go _mess_ it up now by converting to std:string.

probably will use std::string in the future.

thanks
Lee
|:Lee

This topic is closed to new replies.

Advertisement