473,405 Members | 2,354 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,405 software developers and data experts.

Inputing character strings using stream extraction operator

Pmb
How are character strings input from the user. For example: Suppose I have
the code
____________________________________
#include <iostream.h>

int main()
{
char nameString[10];

cout << "Enter your name: ";
cin >> nameString;
cout << "\nThe name you entered was " << nameString<< endl;

return 0;
}
____________________________________

If I run this program and type in "Joe Shmoe" the output will be

------------------------------
Enter your name: Joe Shmoe

The name you entered was Joe
------------------------------

I assume this is because the stream was read up to the blank space between
"Joe" and "Shmoe". How do I get the whole character string read into
nameString?

Pmb
Jul 22 '05 #1
2 1974
Le 28/05/2004 à 23:34:03, Pmb <so*****@somewhere.com> a écrit:
How are character strings input from the user. For example: Suppose I have
the code
____________________________________
#include <iostream.h>

int main()
{
char nameString[10];

cout << "Enter your name: ";
cin >> nameString;
cout << "\nThe name you entered was " << nameString<< endl;

return 0;
}
____________________________________

If I run this program and type in "Joe Shmoe" the output will be

------------------------------
Enter your name: Joe Shmoe

The name you entered was Joe
------------------------------

I assume this is because the stream was read up to the blank space between
"Joe" and "Shmoe". How do I get the whole character string read into
nameString?


Formatted input (ie >>) are based on white space separation, and you
can't change it (AFAIK).

I you not happy with that, you must use unformatted input (get,
getline, read) and do the needed trimming/parsing/etc yourself.

In your case:

#include <iostream>

int main()
{
char nameString[10];

std::cout << "Enter your name: " << std::flush;

if (std::cin.getline(nameString, 10, '\n'))
std::cout << "\nThe name you entered was " << nameString
<< std::endl;
else
std::cout << "Error" << std::endl;

return 0;
}

with a better error handling code

NB: you should use an ISO compiler witch needs std:: namespace and
provide non .h headers.

--
TheDD
Jul 22 '05 #2
"TheDD" <pa*********@pas.de.spam.fr> wrote in message
news:ea***************************@40tude.net...
Le 28/05/2004 à 23:34:03, Pmb <so*****@somewhere.com> a écrit:
How are character strings input from the user. For example: Suppose I have the code
____________________________________
#include <iostream.h>

int main()
{
char nameString[10];

cout << "Enter your name: ";
cin >> nameString;
cout << "\nThe name you entered was " << nameString<< endl;

return 0;
}
____________________________________

If I run this program and type in "Joe Shmoe" the output will be

------------------------------
Enter your name: Joe Shmoe

The name you entered was Joe
------------------------------

I assume this is because the stream was read up to the blank space between "Joe" and "Shmoe". How do I get the whole character string read into
nameString?
Formatted input (ie >>) are based on white space separation, and you
can't change it (AFAIK).


Actually, you can. But it's a somewhat 'advanced' topic,
to do with locales and facets. See Josuttis and/or
Langer & Kreft for details.
I you not happy with that, you must use unformatted input (get,
getline, read) and do the needed trimming/parsing/etc yourself.

In your case:

#include <iostream>

int main()
{
char nameString[10];

std::cout << "Enter your name: " << std::flush;

if (std::cin.getline(nameString, 10, '\n'))
std::cout << "\nThe name you entered was " << nameString
<< std::endl;
else
std::cout << "Error" << std::endl;

return 0;
}

with a better error handling code


IMO better is to use std::getline() to store the input
in a std::string. Then you don't have the size limitations
inherent with an array (std::strings resize automatically
as needed).

std::string nameString;
std::getline(std::cin, nameString);

(You'll need to #include <string> for this.)

-Mike
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Kevin Grigorenko | last post by:
Okay, i've got a file open, call it infile. I've got a simple while(!eof), but say the first line of my file is "<?xml version="1.0">", then it takes two iterations to get the string. first...
2
by: Sonny | last post by:
Hi experts, I am writing a small code to read input from a file, for example my input file is: X Y Z 26.0 28.0 0.0 32.0 8.0 0.0 My code is:...
3
by: Danny Tuppeny | last post by:
Hi all, I'm trying to send a null character as a string delimiter through a TcpClient (code below). It's to connect to this poker bot room: http://games.cs.ualberta.ca/webgames/poker/bots.html...
1
by: yeutim | last post by:
I can't find anything wrong this with this class. Especially, Overloading Stream-Extraction Operator (in blue). Any one know what do I need to fix this problem please reply. Thank you for your...
3
by: sven.suursoho | last post by:
Hello, In main(), the first output API is what I try to achieve. Unfortunately it fails, printing first string as pointer instead of human readable message. Tried to initialize str(""), set new...
5
by: Diego Martins | last post by:
Since C++ (and STL) have many ways to do string concatenation, I want to hear (read) from you how you do to concatenate strings with other strings and other types. The approaches I know are: --...
4
by: seedstorm | last post by:
I am using a HtmlInputFile control in ASP.NET 2.0 to upload a file in a UserControl. After upload, I am examining the HttpPostedFile property of this object to read the bytes of the uploaded file's...
7
by: sam.barker0 | last post by:
Hi, I tried to use the java approach to join 2 strings int w=0; string z="blah " + w + "blah " but this gives me an error but this works string z("blah"); z+=w;
5
by: newbarker | last post by:
Hello all, #include <sstream> #include <iostream> int main() { std::istringstream iss("6.5e"); double size; char fit;
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: 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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.