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

Inputing character strings using stream extraction operator

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.