By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,417 Members | 1,057 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,417 IT Pros & Developers. It's quick & easy.

cin and strings

P: n/a
Hello,

I have the following construction:
#define STRINGLENGTH 30
...
char* Name;
....
Name = new char[STRINGLENGTH];
cin>>Name;
cout<<Name;

But the problem is, when I enter a string that contains spaces (eg: "it's
raining"), only the first part of this string is printed (so for the example
"it's" would be printed). Is there a way to avoid this and to read and pront
the entire string?

Thank you very much,

Yannick
Jun 23 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Yannick De Koninck wrote:
Hello,

I have the following construction:
#define STRINGLENGTH 30
..
char* Name;
Why not use std::string?
...
Name = new char[STRINGLENGTH];
cin>>Name;
What happens if the user enters a string that is longer than 29 characters?
cout<<Name;

But the problem is, when I enter a string that contains spaces (eg: "it's
raining"), only the first part of this string is printed (so for the
example "it's" would be printed).
Well, operator>> only reads up to white space, so that's the intended
behavior.
Is there a way to avoid this and to read and pront the entire string?


Try std::getline, or if you insist on using raw char arrays, cin's getline
member.

Jun 23 '06 #2

P: n/a
Yannick De Koninck <Ya***************@ugent.be> wrote:
Hello,

I have the following construction:
#define STRINGLENGTH 30
..
char* Name;
...
Name = new char[STRINGLENGTH];
cin>>Name;
cout<<Name;

But the problem is, when I enter a string that contains spaces (eg: "it's
raining"), only the first part of this string is printed (so for the example
"it's" would be printed). Is there a way to avoid this and to read and pront
the entire string?


The problem is that the >> operator by default stops at a whitespace, so
it only reads up until the space after "it's". I believe there is a
cin.getline() function that you can use. However, I prefer to use
std::string, which automagically takes care of the length of the string.

For example,

#include <iostream>
#include <string>

int main()
{
std::string name;
std::getline(std::cin, name); // note that this is different from
// the member function cin.getline()
// that I mentioned above
std::cout << name << '\n';
}

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jun 23 '06 #3

P: n/a
In article <e7**********@gaudi2.UGent.be>,
Ya***************@ugent.be says...
Hello,

I have the following construction:
#define STRINGLENGTH 30
..
char* Name;
...
Name = new char[STRINGLENGTH];
cin>>Name;
cout<<Name;

But the problem is, when I enter a string that contains spaces (eg: "it's
raining"), only the first part of this string is printed (so for the example
"it's" would be printed). Is there a way to avoid this and to read and pront
the entire string?


getline -- and you almost certainly want to use an
std::string instead of dynamically allocating an array
like you have above.

std::string Name;

std::getline(std::cin, Name);
std::cout << Name;

If you really want to use operator>> to read the string,
there are a couple ways of handling that as well. See the
line and line_reader classes at:

http://tinyurl.com/nuhrg

for a couple of possibilities.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 23 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.