2015-11-07 101 views
-2

我的程序提示用户输入文件名,并使用getline将其放入我的字符串变量中。 getline不会将用户的输入存储到文件名中。为什么?getline(cin,str)不起作用

int main() { 
string fileName; 
cout << "==============================================================\n" 
<< "|  Welcome to the Automatic Maze Path Finder!  |\n" 
<< "=============================================================\n" 
<< "\nEnter the name of the Maze configuration file: "; 
getline(cin, fileName); 

此外,当使用cout语句来测试它将输出放到当前命令行。 例如: user @ computer:〜$ //程序输出由于某种原因而出现在这里? 为什么这样做?

+0

你想在哪里输出控制台? –

+0

你怎么知道它没有存储到'filename'中?你有没有用调试器通过它? – MicroVirus

+0

你会遇到这个问题吗? https://stackoverflow.com/questions/1744665/need-help-with-getline –

回答

0

std::cin是标准用户输入的表示,并且std::cout是标准输出的表示。如果您希望将输入/输出读取/写入不同的内容,则必须使用该系统(例如std:fstream)。

您的代码似乎是fine to me

+0

你有没有使用任何特殊命令来编译你的?编译器版本/ C++版本会有所作为吗?我的工作不正常! – helpme

+0

你在用什么。我使用在线编译器(gcc-5.1)对其进行编译,但Microsoft CL(使用Visual Studio分发)也可以使用。 – Zereges

+0

我用g ++在Linux中编译 – helpme