Inputing character strings using stream extraction operator

P

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
 
T

TheDD

Le 28/05/2004 à 23:34:03 said:
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.
 
M

Mike Wahler

TheDD said:
Le 28/05/2004 à 23:34:03 said:
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
 

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

No members online now.

Forum statistics

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

Latest Threads

Top