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++;
}
}
fstreamandstringstreamshould function the same for your use case here. You can use>>with thefstream. Is there any reason you want this in astringstream?std::getline.std::getlinethat accepts two stream objects.std::getline. Read the documentation. And you might want to dig out the documentation forstd::stringstreamas well.