cin and strings

Y

Yannick De Koninck

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
 
R

Rolf Magnus

Yannick said:
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.
 
M

Marcus Kwok

Yannick De Koninck said:
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';
}
 
J

Jerry Coffin

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top