tags:

views:

600

answers:

3

When you enter a space ' ' while typing into cin, it will take the first string before the space as the first value and the later one as the next.

So let's say we have this code:

cout << "Enter your Name";
cin >> name;

cout << "Enter your age";
cin >> age;

Now, let's say a user enters "John Bill".

It would take his name to be John and his age to be Bill.

Is there a way to:

  1. Have the line automatically change it from a ' ' to a '_'?

  2. Have it so that it will read that line as that line and have the space ' ' read as a normal character?

+2  A: 

You want to use cin.getline() which can be used like this:

cin.getline(name, 9999, '\n');

And will include everything up to the newline or 9999 characters. This only works for c-style char arrays though.

getline(cin, name, '\n');

will work for std::strings.

If you want to replace the space with an underscore you're going to have to do that manually. Assuming you are using std::string you could make a function like this:

void replace_space(std::string &theString)
{
    std::size_t found = theString.find(" ");
    while(found != string::npos)
    {
        theString[found] = '_';
        found = theString.find(" ", found+1);
    }
}
Niki Yoshiuchi
Or std::replace(theString.begin(), theString.end(), ' ', '_');
Steve Jessop
A: 

When you do "cin >>", you are calling cin.get with the ios::skipws flag set by default. Call cin.get explicitly to have it include whitespaces.

cin.get(name, strlen(name))

Source: http://minich.com/education/wyo/cplusplus/cplusplusch10/getfunction.htm

Kai
The page you reference should be closed down a s a danger to learning programmers. And even it doesn't advocate the code you provide.
anon
I've looked through the page and it seems to be a well-explained reference to me. Can you be clearer so that others understand the danger in a seemingly accurate page?
Kai
+4  A: 

To read a line in C++:

#include <iostream>
#include <string>
using namespace std;

int main() {
    cout << "Enter some stuff: " ;
    string line;
    getline( cin, line );
    cout << "You entered: " << line << endl;
}
anon
No need to call the string header file/library. Better to use a char array, or better yet, a char* to do the job.
Sev
Wrong - in more ways than will fit into this comment box.
anon
Useless comment, as it does no one any good. If you want to tell the world that I'm wrong, point them as to why I'm wrong.
Sev
Oh, do go away. And on your way think about how you can read a string of characters (of lets say length 100) into a char * (of length 4, on most platforms).
anon
You wanted to store 100 characters as an address? Funny man. I said make USE of a char *. On your way out, look up how to use character pointers. Let me help you there as well: http://lmgtfy.com/?q=character+pointers
Sev
Sev, you're starting to make an ass out of yourself. :|
GMan
Unfortunately, Neil brought that out of me.
Sev
How so? If I recall, you posted a comment with an incorrect argument.
GMan
By telling me "do go away" - that's much worse - arguments and discussions are much more beneficial than slander and ignorance. Even if my argument was incorrect.
Sev