Reputation: 1
I had this program working a while ago but I recently came back to look at it for another project I am working on and now I am getting errors while trying to use getline()
The other file is just a text file that has the player's name, their position and other data about them as such:
Bill Quarter_Back 70 0 8754 0 573
before adding :: infront of the getline function I was getting the error
error: no matching function for call to ‘getline(std::ifstream&, int&, char)’
getline(input, p[i].position, ' '); "
and I don't know why.
Here's the code:
#include <iostream>
#include <fstream>
#include <string>
struct player{
int position, touchDowns, catches, passingYards, recievingYards, rushingYards;
std::string name;
};
using namespace std;
void printPlayer(player p[], ifstream &input);
void createArray(player p[], ifstream &input);
int main() {
ifstream input;
player p[10];
int choice;
while(choice != 99){
cout << "Select one of the following options: " << "1: To print a player's data\n2: To print the entire data\n3: To update a player's touch downs\n4: To update a player's number of catches\n5: To update a player's passing yards\n6: To update a player's receiving yards\n7: To update a player's rushing yards\99: To quit the program" <<endl;
cin >> choice;
switch (choice){
case 1:
printPlayer(p, input);
}
if(choice == 99){
break;
}
}
return 0;
}
void createArray(player p[], ifstream &input){
for(int i =0;i < 10; i++){
getline(input, p[i].name, ' ');
getline(input, p[i].position, ' ');
getline(input, p[i].touchDowns, ' ');
getline(input, p[i].catches, ' ');
getline(input, p[i].passingYards, ' ');
getline(input, p[i].recievingYards);
getline(input, p[i].rushingYards);
}
}
void printPlayer(player p[], ifstream &input){
int wantedPlayer;
string guyname;
cout << "Enter players name: ";
cin >> wantedPlayer;
cout << endl;
for(int i =0; i < 10; i++){
guyname = p[i].name;
cout << p[i].name << " " << p[i].position << " " << p[i].touchDowns << " " << p[i].catches << " " << p[i].passingYards << " " << p[i].recievingYards << " " << p[i].rushingYards << "\n" << endl;
}
}
Upvotes: 0
Views: 3595
Reputation: 595402
Without the using namespace std;
statement (which you should not use), your calls to getline()
would not be calling C++’s std::getline()
, but C’s getline()
from <stdio.h>
:
ssize_t getline(char **lineptr, size_t *n, FILE *stream);
Which is why you were getting errors about converting std::ifstream
to char**
. To fix that error, make sure you use getline()
from the std
namespace, eg:
void createArray(player p[], std::ifstream &input){
for(int i =0;i < 10; i++){
std::getline(input, p[i].name, ' ');
...
}
}
However, std::getline()
can’t read input into an int
, only into a std::string
. If you want to read a formatted integer from text input, you will have to convert the std::string
using std::stoi()
or equivalent, eg:
void createArray(player p[], std::ifstream &input){
std::string temp;
for(int i =0;i < 10; i++){
...
std::getline(input, temp, ' ');
p[i].position = std::stoi(temp);
...
}
}
Otherwise, use operator>>
instead:
void createArray(player p[], std::ifstream &input){
for(int i =0;i < 10; i++){
input >> p[i].name >> p[i].position >> ...;
input.ignore(std::numeric_limits<std::streamsize>::max(), ‘\n’);
}
}
Upvotes: 3