Getting user input

I want to get a line of the user input.

What I am doing now is:

string str;
cin>>str;

However, if the input is :This is a input.

str will only be "This".

How do I get the entire line?

P.S. I am using g++ as compiler.
Please don't use anything other than STL to do it.
i think that gets(str) will do it for you. the gets stop when you press enter.
you have to include the #include <cstdio> also a header for gets to work.
Last edited on
Typically whenever you hear an "always/never" rule you can ignore it as nonsense.

This is an exception to that. Never use gets().

The cin stream is designed to tokenize input on whitespace. If you want an entire line you need to use the string getline() function.
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>
using namespace std;

int main()
  {
  string name;
  cout << "What is your full name> ";
  getline( cin, name );
  cout << "Hello " << name << "!\n";
  return 0;
  }


Hope this helps.
what's wrong with get()?
Last edited on
There is nothing wrong with get().

But gets(), OTOH, is a buffer overflow waiting to happen. That and it is a C function. If you are programming in C++ then do things the C++ way --you'll get type checking and all sorts of neat stuff.
Thanks
Topic archived. No new replies allowed.