使用getline作为输入将Fstream作为输入的问题
请原谅我以前发布过。我尝试搜索此问题,但无法描述我的问题。
我现在正在从事一个项目,这使我们在图中找到连接。该程序可以接受终端或输入文件的用户输入。我创建了一个驱动程序函数,该函数将ISTream作为参数,该参数取决于用户是从终端输入命令还是从输入文件中传递它们。
我认为我的问题来自“ getline”。我使用getline,因为其中一些顶点的名称中有一个空间。当我使用CIN时,该程序有效,但在FSTREAM上存在问题。
该程序根据用户的输入执行不同的搜索。如果我将getline与CIN一起使用,则该程序可以理解命令,但它与Getline和fstream不起作用。如果我使用>>将fstream传递到字符串。操作员,没有问题,但是读取搜索查询的getline命令之后以空白的形式出现。我还尝试过Cin.ignore()无济于事。
这是所讨论的功能:
void six_degrees::start(string filename, istream &input, ostream &output_type)
{
populate(filename);
string command;
string source;
string dest;
while (input)
{
input >> command; // used to be a getline() but had issues reading when passing fstream
if (command == "dfs")
{
input.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // didn't work
getline(input, source);
getline(input, dest);
if (database_check(source) && database_check(dest))
{
print_path(output_type, degrees.dfs(source, dest), source, dest);
}
}
else if (command == "bfs")
{
getline(input, source);
getline(input, dest);
if (database_check(source) && database_check(dest))
{
print_path(output_type, degrees.report_path(source, dest), source, dest);
}
}
else if (command == "not")
{
getline(input, source);
getline(input, dest);
string list;
vector<Artist> exclude;
while (list != "*")
{
getline(input, list);
exclude.push_back(list); // uses getter to return artist instance
}
if (database_check(source) && database_check(dest))
{
if (exclusion_check(exclude))
{
print_path(output_type, degrees.not_search(source, dest, exclude), source, dest);
}
}
}
else if (command == "quit")
{
break;
}
else
{
cout << command << " is not a command. Please try again" << endl;
}
}
}
Please excuse me if this has been posted before. I tried searching for this but couldn't describe my problem.
I am working on a project right now that has us finding connections in a graph. The program can either accept user input from the terminal or from an input file. I have created a driver function that takes an istream as a parameter which is dependent on whether or not the user is inputting commands from the terminal or passing them in from an input file.
I think my problem is coming from "getline". I use getline because some of the vertices have names with a space in them. The program works when I use cin, but has problems with fstream.
The program executes different searches depending on the user's input. If I use getline with cin, the program can understand the commands but it does not work with getline and fstream. If I pass the fstream to a string using the >> operator, there's no issue, but the getline commands that read the search queries come up as blanks after that. I've also tried cin.ignore() to no avail.
Here's the function in question:
void six_degrees::start(string filename, istream &input, ostream &output_type)
{
populate(filename);
string command;
string source;
string dest;
while (input)
{
input >> command; // used to be a getline() but had issues reading when passing fstream
if (command == "dfs")
{
input.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // didn't work
getline(input, source);
getline(input, dest);
if (database_check(source) && database_check(dest))
{
print_path(output_type, degrees.dfs(source, dest), source, dest);
}
}
else if (command == "bfs")
{
getline(input, source);
getline(input, dest);
if (database_check(source) && database_check(dest))
{
print_path(output_type, degrees.report_path(source, dest), source, dest);
}
}
else if (command == "not")
{
getline(input, source);
getline(input, dest);
string list;
vector<Artist> exclude;
while (list != "*")
{
getline(input, list);
exclude.push_back(list); // uses getter to return artist instance
}
if (database_check(source) && database_check(dest))
{
if (exclusion_check(exclude))
{
print_path(output_type, degrees.not_search(source, dest, exclude), source, dest);
}
}
}
else if (command == "quit")
{
break;
}
else
{
cout << command << " is not a command. Please try again" << endl;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@Whozcraig弄清楚了。我正在使用引起该问题的Windows Powershell。我登录了使用Linux的远程服务器(无论如何它们都应该是),但没有问题。
@WhozCraig figured it out. I was using windows powershell which caused the issue. I logged into my schools remote server that uses linux (where they're supposed to be anyway) and didn't have a problem.