Connecting Tech Pros Worldwide Help | Site Map

Char input gets cut off when there is whitespace

Member
 
Join Date: Aug 2008
Location: South essex, England
Posts: 82
#1: Aug 27 '08
Hi, Im using a server and and a client, and the client can send messages to the server, but the messages gets cut when theres a whitespace.

Say I typed:
Hello my name is adam.

The server will catch it as: Hello

I think this has to do with cin, or the char[] method.

heres the client message code:
Expand|Select|Wrap|Line Numbers
  1.  
  2. char message[1000];
  3.                for(;;){
  4.  
  5.                     std::cout << "Message: ";
  6.                     std::cin >> message;
  7.                     if(strstr(message,"\\exit")){send(mysoc, "\\Disconnect", 1000, 0); break;}
  8. send(mysoc, message,1000, 0);
  9.                 }
  10.  
Server catch code:

Expand|Select|Wrap|Line Numbers
  1. char buf[1000];
  2. for(;;){
  3. if((recv(clientsocket, buf, 1000, 0)) == -1)
  4. { message.write("!:Error recieving client message<br>\n"); break;}
  5. else { message.write(buf);}
  6. }
Can someone help me to get the client to send a full message.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Aug 27 '08

re: Char input gets cut off when there is whitespace


When you do 'cin >> buf' the istream will be read until a white space character
is read from the stream. Use the streams' getline() method instead if you want
to read an entire line from the stream.

kind regards,

Jos
Member
 
Join Date: Aug 2008
Location: South essex, England
Posts: 82
#3: Aug 27 '08

re: Char input gets cut off when there is whitespace


Thanks, what header, and class does it come under?
Needs Regular Fix
 
Join Date: Sep 2007
Location: The Netherlands
Posts: 427
#4: Aug 27 '08

re: Char input gets cut off when there is whitespace


<iostream>. Check reference.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#5: Aug 27 '08

re: Char input gets cut off when there is whitespace


Quote:

Originally Posted by Adam01

Thanks, what header, and class does it come under?

The same header file and class you're using already; just a different function.
Here's a nice online reference.

kind regards,

Jos
Member
 
Join Date: Aug 2008
Location: South essex, England
Posts: 82
#6: Aug 27 '08

re: Char input gets cut off when there is whitespace


Ok, Thanks for all your help, works smooothly.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#7: Aug 27 '08

re: Char input gets cut off when there is whitespace


Quote:

Originally Posted by arnaudk

<iostream>. Check reference.

Maybe we should get rid of those old stickies and post just one new one that
mentions this (and other) useful links. Similar to what I did in the Java forum ...

kind regards,

Jos
Needs Regular Fix
 
Join Date: Sep 2007
Location: The Netherlands
Posts: 427
#8: Aug 28 '08

re: Char input gets cut off when there is whitespace


Quote:

Originally Posted by JosAH

Maybe we should get rid of those old stickies and post just one new one that
mentions this (and other) useful links. Similar to what I did in the Java forum ...

kind regards,

Jos

There's an article in the how-tos with C/C++ links but I think some of them are a little outdated, and probably few people check there. So yes, I think that's a great idea, so long as it's updated now and then.
Reply