1

I am making a bowling program for school that stored scores in a text file with the format:

paul 10 9 1 8 1, ...etc
jerry 8 1 8 1 10 ...etc
...etc

I want to read the file into a stringstream using getline() so I can use each endl as a marker for a new player's score (because the amount of numbers on a line can be variable, if you get a spare or strike on round 10). I can then read the stringstream using >> to get each score and push it into a vector individually.

However, when trying to use getline(fstream, stringstream), I get an error

no instance of overloaded function "getline" matches the argument list -- argument types are: (std::fstream, std::stringstream)

How can I make this work?

My code looks like this:

#include <iostream>
#include <vector>
#include <fstream>
#include <exception>
#include <string>
#include <sstream>

using namespace std;

//other parts of the program which probably don't matter for this error

vector <int> gameScore;
vector<string> playerName;
int i = 0;
int j = 0;
string name;
int score;
stringstream line;

while (in.good()){ //in is my fstream
    playerName.push_back(name);
    cout << playerName[i] << " ";
    i++;
    getline(in, line);

    while (line >> score){
        gameScore.push_back(score);
        cout << gameScore[j] << " ";
        j++;
    }
}
6
  • fstream and stringstream should function the same for your use case here. You can use >> with the fstream. Is there any reason you want this in a stringstream? Commented Dec 3, 2021 at 22:49
  • 1
    I need to getline() so i grab all the scores up to the endl (because the ammount of numbers on a line is variable) then read each individual score and push it into the vector until the stringstream is empty (the filestream won't be empty yet because there can be more lines of scores) IF I just getline() the fstream then I will have no way to separate each score individually and push it into the vector. Commented Dec 3, 2021 at 22:59
  • 1
    You should read the documentation for std::getline. Commented Dec 3, 2021 at 23:13
  • There is no form of std::getline that accepts two stream objects. Commented Dec 3, 2021 at 23:20
  • 1
    @Joe -- nobody said you can't use std::getline. Read the documentation. And you might want to dig out the documentation for std::stringstream as well. Commented Dec 3, 2021 at 23:29

1 Answer 1

3

You can't use std::getline() to read from a std::ifstream directly into a std::stringstream. You can only read into a std::string, which you can then assign to the std::stringstream, eg:

vector<int> gameScore;
vector<string> playerName;
string name, line;
int score;

while (getline(in, line)){
    istringstream iss(line);
    iss >> name;
    playerName.push_back(name);
    cout << name << " ";

    while (iss >> score){
        gameScore.push_back(score);
        cout << score << " ";
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.