Kal
Kal

Reputation: 1

Issues with passing fstream as an input using getline

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;
        }
    }
}

Upvotes: 0

Views: 107

Answers (1)

Kal
Kal

Reputation: 1

@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.

Upvotes: 0

Related Questions