Skip to main content
GameDev.net gamedev.net
🔒 Locked

cin.get and sockets

Started by Infuscare Mar 1, 2004 at 9:46 PM 7 replies 500+ views
Original Post
Infuscare
Infuscare
I dont get it, it skips right through the cin.get statement This is the main loop in the program, message being a char [128] s is a socket (that works)... when I run it like this, if i put more than one word, it adds an empty "message to be sent:" queue (skips it). What the other side receives is ''(the first word) (\n) (all the rest without spaces)'' for example if i wrote: "hello willing world" it would put 2 empty prompts the other side would display: hello willingworld
do
{
	cout << "Message to be sent: "; 
	cin >> message;
	cout << endl;
	
	if(message[0]!= ''\0'')
		send(s, message, (int)strlen(message),0);
		
	if(ret == SOCKET_ERROR)
	{
		cout << "Message sending error occured" << endl;
		system("Pause");
		end = -1;
		break;
	}
}while(true);
visual studio.net c/c++ (obviously) i don''t mind giving the full source if needed, its not that long
___________"You're just jelous because the voices only talk to me"
Beer Hunter
Beer Hunter
Using cin >> to read a string will only read one whitespace-delimited token from the input stream. You probably want to read an entire line, so use getline:

string line;
getline(cin, line);
if (line.size() > 0) send(s, line.c_str(), line.size(), 0);


or

char line[1024];
cin.getline(line, 1024);
if (strlen(line) > 0) send(s, line, strlen(line), 0);


You also noted that all the other terms get joined together without whitespace. Calling send repeatedly and quickly enough (on a TCP connection) will cause the data to be concatenated and sent as one packet. This is reasonable, since TCP is a stream-based protocol. You''ll usually want to send extra information so that the other end can identify where one block of information ends and another starts. This can be done by sending a delimeter (''\n'' will do here) or by prefixing a block of information with its size.
Infuscare
Infuscare
Thanks, that improved things, but it still goes through the loop once before it asks for input... after filling in the ip and port, before (which work fine) it outputs:

connecting...done
Message to be sent:
Message to be sent:_

-the _ being where the cursor is...
I don't understand why it would ever skip through a cin... care to venture a guess?

also, any idea why it just skipped through the cin.get() ?

_________________________________________
"Why, why why does everyone ask ''why'' when ''how'' is so much more fun"
-Spawn 1997

Ps. Yes I know what I'm saying kind of contradicts my signature

[edited by - Infuscare on March 2, 2004 10:10:32 PM]
___________"You're just jelous because the voices only talk to me"
Ademan555
Ademan555
Infuscare, i had the EXACT same problem... i cant figure it out though... i just moved to windows as quick as i could, i wonder if the stdio would have the same problems...
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out hi
_Twiggie_
_Twiggie_
Im not 100% sure this is your problem, but try this out and comment/uncomment the specified line to see what happens.



#include <iostream>
#include <string>

int main(int argc, char *argv[])
{

char input = 0;

std::cout << "input first: ";
std::cin.get( input );
std::cout << "\n" << input << std::endl;



std::cin.ignore( ); // comment out this line and see the difference



std::cout << "input second: ";
std::cin.get( input );
std::cout << "\n" << input << std::endl;

return EXIT_SUCCESS;
}


Further information


[edited by - _Twiggie_ on March 2, 2004 11:24:05 PM]
Ademan555
Ademan555
Well, i think what hes saying is that it will iterate through his loop ONLY ONCE, at least thats how it was for me, i could sucessfully send 1 message, then it would konk out, I personally added the ignore in an attempt to get it to work... it didnt work (for ME) without it either... thankx tho, maybe ittl help him
-Dan
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out hi
Infuscare
Infuscare
nice, when I put in the cin.ignore() it worked, thanks

It seems to have done the trick, but I still don''t know what went wrong, or what ignore really does, i mean I read the definition, but what is it, just empty the stream...? but otherwise why did it skip through it?

_________________________________________
"The fairest thing we can experience is the mysterious. It is the fundamental emotion which stands at the cradle of true art and true science."
-Albert Einstein
___________"You're just jelous because the voices only talk to me"
_Twiggie_
_Twiggie_
Glad to hear worked. Instead of attempting to explain what it does I would rather quote something:


Using cin.getline

The getline member function is similar to the get function. Both functions allow a third argument that specifies the terminating character for input. The default value is the newline character. Both functions reserve one character for the required terminating character. However, get leaves the terminating character in the stream and getline removes the terminating character. found here

Edit: So in essence, using the default arguments for the ignore method will skip the terminating character currently in the stream. Therefore allowing you to do what you initially expected.

Hope that helps.


[edited by - _Twiggie_ on March 3, 2004 1:41:19 AM]
Beer Hunter
Beer Hunter
quote:
Original post by Infuscare
nice, when I put in the cin.ignore() it worked, thanks

It seems to have done the trick, but I still don''t know what went wrong, or what ignore really does, i mean I read the definition, but what is it, just empty the stream...? but otherwise why did it skip through it?
You read the IP and port, but they were followed by a newline character which was left in the input stream. The subsequent call to getline only saw the newline character, so it returned an empty string. Calling ignore removes the newline character from the stream.

Topic Locked

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

Sign in to reply to this topic.