Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't cin >> string working with Visual C++ 2010? [closed]

Tags:

c++

string

cin

#include <iostream>

using namespace std;

int main() {
    string s;
    cin >> s;
    cout << "Hello World!";
}

This isn't working. Why?

like image 422
Ivan Avatar asked Mar 17 '11 17:03

Ivan


People also ask

Why CIN get is not working?

get() extracts that newline from the stream, and the second wait waits for input because the stream is now empty. If you had only the one cin. get() call, it would extract the newline immediately and continue, and since there is nothing after that cin. get() call, the program terminates (as it should).

Does CIN work with string?

Inputting a string You can use cin but the cin object will skip any leading white space (spaces, tabs, line breaks), then start reading when it comes to the first non-whitespace character and then stop reading when it comes to the next white space. In other words, it only reads in one word at a time.

How do you terminate a CIN?

On Windows you'd use Ctrl-Z, on UNIXes you'd use Ctrl-D.


1 Answers

Because you forgot to #include <string>

like image 179
Alexander Gessler Avatar answered Sep 22 '22 12:09

Alexander Gessler