0

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?

Charles Sprayberry
  • 7,741
  • 3
  • 41
  • 50

4 Answers4

4

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;
}
  • 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 Jun 15 '09 at 22:55
  • 10
    Wrong - in more ways than will fit into this comment box. –  Jun 15 '09 at 23:00
  • 2
    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 Jun 15 '09 at 23:03
  • 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). –  Jun 15 '09 at 23:05
  • 1
    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 Jun 15 '09 at 23:15
2

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
  • 16,883
  • 1
  • 35
  • 44
0

I would suggest using the std::string as it is safer. Using a char * + allocate memory via malloc is dangerous and it has to be checked for allocation. However you should check this link for more information on when the other is beneficial https://stackoverflow.com/a/6117751/1669631

Community
  • 1
  • 1
0

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
  • 9,444
  • 6
  • 46
  • 61
  • 1
    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. –  Jun 15 '09 at 22:52
  • 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 Jun 16 '09 at 14:42