Cin没有操作数>> [英] Cin has no operand >>
问题描述
我不明白为什么这不工作。由于某种原因,我得到错误:
I don't understand why this isn't working. For some reason I'm getting the error:
错误C2678:binary'>类型'std :: istream'的手操作数(或没有可接受的转换)
我在Visual Studio2010 C ++ Express如果这有帮助。不知道为什么它把我这个错误我做了其他程序使用 cin
...
I'm doing this in Visual Studio2010 C++ Express if that helps. Not sure why its handing me this error I've done other programs using cin
...
我的代码:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main(int argc, char* argv){
string file;
if (argc > 1)
{
file = argv[1];
}
else
{
cout << "Please Enter Your Filename: ";
cin >> file;
}
}
推荐答案
include < string>
在上面我建议你使用getline代替>>将停止在第一词在您的输入。
On top of that I suggest you use getline instead as >> will stop at the first word in your input.
示例:
std::cin >> file; // User inputs C:\Users\Andrew Finnell\Documents\MyFile.txt
结果是C:\Users\Andrew,非常意外,考虑到数据没有消耗,直到换行符,并且下一个std :: string读取将自动消耗并填充Finnell\Documnts\MyFile .txt
The result is "C:\Users\Andrew", quite unexpected considering that the data is not consumed until the newline and, the next std::string read will automatically be consumed and filled with "Finnell\Documnts\MyFile.txt"
std::getline(std::cin, file);
这将消耗所有文本,直到换行符。
This will consume all text until the newline.
这篇关于Cin没有操作数>>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!