473,395 Members | 1,631 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Char input gets cut off when there is whitespace

83
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.
Aug 27 '08 #1
7 2812
JosAH
11,448 Expert 8TB
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
Aug 27 '08 #2
Adam01
83
Thanks, what header, and class does it come under?
Aug 27 '08 #3
arnaudk
424 256MB
<iostream>. Check reference.
Aug 27 '08 #4
JosAH
11,448 Expert 8TB
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
Aug 27 '08 #5
Adam01
83
Ok, Thanks for all your help, works smooothly.
Aug 27 '08 #6
JosAH
11,448 Expert 8TB
<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
Aug 27 '08 #7
arnaudk
424 256MB
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.
Aug 28 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Don | last post by:
Can't figure this one out. The "validate" function below checks to make sure the field "names" is not null. "validate" actually calls "isEmpty", which scans the field. Here's the situation. ...
9
by: Sathyaish | last post by:
I noticed that gets() reads into the buffer even if the you've not allocated enough memory. For instance, if you do: char *str=(char*)malloc(sizeof(char)); printf("Enter something about...
7
by: arkobose | last post by:
hey everyone! i have this little problem. consider the following declaration: char *array = {"wilson", "string of any size", "etc", "input"}; this is a common data structure used to store...
10
by: yaniv.dg | last post by:
hi all, i'm bumping into smething very starnge its very simple code but from some reason the program is acting strange this is my code: #include<stdio.h> #include<conio.h> void main(void) {...
7
by: william | last post by:
I wonder how 'main(int argc, char ** argv)' is implemented? How does it get the string literals separated by whitespace from the stdin stream? And is there any difference between 'char**' and...
4
by: Alan | last post by:
How do I convert the type const char* to type char*? I have an input string ("input_string") of class string. I need to convert this to type char* in order to use it in a call to the strtok()...
22
by: kudruu | last post by:
Hello, I am having a problem in Microsoft Visual C++ with some code I have written (I am a bit of a novice so I appologize if this is a very mundane problem). This is a segment from one of the...
59
by: David Mathog | last post by:
Apologies if this is in the FAQ. I looked, but didn't find it. In a particular program the input read from a file is supposed to be: + 100 200 name1 - 101 201 name2 It is parsed by reading...
13
by: FerrisUML | last post by:
Hello everyone! I new to C and am having the following problem. In the below program, the last scanf is being ignored and the program exits. Can anyone see anything that im doing wrong? Thanks...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.