473,395 Members | 1,539 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.

cin and strings

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
3 2104
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Ravi | last post by:
Hi, I have about 200GB of data that I need to go through and extract the common first part of a line. Something like this. >>>a = "abcdefghijklmnopqrstuvwxyz" >>>b = "abcdefghijklmnopBHLHT"...
17
by: Gordon Airport | last post by:
Has anyone suggested introducing a mutable string type (yes, of course) and distinguishing them from standard strings by the quote type - single or double? As far as I know ' and " are currently...
16
by: Paul Prescod | last post by:
I skimmed the tutorial and something alarmed me. "Strings are a powerful data type in Prothon. Unlike many languages, they can be of unlimited size (constrained only by memory size) and can hold...
4
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three...
25
by: Rainmaker | last post by:
Hi, Can anyone tell me an efficient algorithm to sort an array of strings? Keep in mind that this array is HUGE and so the algorithm should me efficient enough to deal with it. Thanks
6
by: Broeisi | last post by:
Hello, I wrote the tiny progam below just to understand arrays and strings better. I have 2 questions about arrays and strings in C. 1. Why is it that when you want to assign a string to an...
2
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be...
19
by: pkirk25 | last post by:
I wonder if anyone has time to write a small example program based on this data or to critique my own effort? A file called Realm List.html contains the following data: Bladefist-Horde...
95
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.