2

我是 C++ 的新手。我在窗口 7 中使用 Cygwin 和 eclipse,我正在尝试编写一个程序来要求用户输入一个字符串并打印出来。我有下面的代码。

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

int main() {
    string mystr;
    cout << "What's your name? ";
    getline (cin, mystr, '\n');
    cout << "Hello " << mystr << ".\n";
    return 0;
}

我也尝试:

getline(cin, mystr);

他们都给了我一个如下所示的输出。

输出:

What's your name? nick
Hello nick
.

为什么句号转到下一行?有人可以告诉我发生了什么并教我如何解决它。

4

1 回答 1

0

In gcc 4.6.3

getline (cin, mystr);

and

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

both are printing the string as you are expecting.

于 2013-04-11T16:47:07.103 回答