I was assigned a problem for class, and even though the due date has passed and I kinda doubt I'll get credit a month later, I would still like to do it for the learning aspect of it. Anyways, my program is basically meant to read from a file, store the information into a structure, do some stuff with that information, then update the text file that was storing the information. I've only gotten as far as storing the info into the structure at the moment.
However, I keep getting errors and I'm not sure what I'm doing wrong. I took a look at my past assignment using structures and everything seems correct, but then again that file wasn't including a .h file.
Please Note: Preferably could you give me hints at what I should be doing, rather than telling me directly what I'm doing wrong? I read:
http://www.cplusplus.com/forum/beginner/1/ and I realized / agree with that it's probably the best way to learn. At least until I've tried and still can't figure out the issue.
Lastly, please note that the error line numbers may be incorrect as I've removed some of the notes to myself.
Errors:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
nba.cpp: In function ‘int main()’:
nba.cpp:95:5: error: ‘readPData’ was not declared in this scope
readPData(pFile, pList, PMAX);
^~~~~~~~~
nba.cpp:95:5: note: suggested alternative: ‘realpath’
readPData(pFile, pList, PMAX);
^~~~~~~~~
realpath
nba.cpp: In function ‘void readPData(std::ifstream, playerData&, int)’:
nba.cpp:114:45: error: no match for ‘operator[]’ (operand types are ‘playerData’ and ‘int’)
std::getline(playersFile, playerList[i].pName);
^
nba.cpp:115:45: error: no match for ‘operator[]’ (operand types are ‘playerData’ and ‘int’)
std::getline(playersFile, playerList[i].tName);
^
nba.cpp:116:31: error: no match for ‘operator[]’ (operand types are ‘playerData’ and ‘int’)
std::cin >> playerList[i].points;
^
nba.cpp:117:31: error: no match for ‘operator[]’ (operand types are ‘playerData’ and ‘int’)
std::cin >> playerList[i].rebounds;
^
nba.cpp:118:31: error: no match for ‘operator[]’ (operand types are ‘playerData’ and ‘int’)
std::cin >> playerList[i].assists;
^
|
nba.h file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#ifndef __nba_struct_H_INCLUDED__
#define __nba_struct_H_INCLUDED__
#include <iostream>
#include <string>
#include <fstream>
struct playerData {
private:
// variables generally that are used in struct functions
std::string pName; // player name
std::string tName; // team name
double points;
double rebounds;
double assists;
public:
// functions
void readPData(std::ifstream playersFile, playerData& playerList, const int maxPlayers);
};
#endif
|
nba.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
#include <iostream>
#include <string>
#include <fstream>
#include "nba.h"
int main() {
// Declaring player list array which contains all player "objects" for the struct.
const int PMAX = 100;
playerData pList[PMAX];
// Opening players.txt file:
std::ifstream pFile; // input file
std::string pFileName = "players.txt";
pFile.open(pFileName);
// Reading player data from players.txt:
readPData(pFile, pList, PMAX);
return 0;
}
// playerData& playerList is being passed by reference. Meaning it SHOULD update the value everywhere without needing to return anything(?)
// Note: It is called playerList here. But in main() it's called pList.
void readPData(std::ifstream playersFile, playerData& playerList, const int maxPlayers) {
// While players file has not reached the end of file:
for (int i = 0; playersFile.eof() == false; i++) {
// playerList[i] is a player in the list. And playerList[i] has it's own parts (such as pName, tName, etc).
// This for loop will go through all players in the file and store their info into the array.
std::getline(playersFile, playerList[i].pName);
std::getline(playersFile, playerList[i].tName);
std::cin >> playerList[i].points;
std::cin >> playerList[i].rebounds;
std::cin >> playerList[i].assists;
}
}
|
Help would be greatly appreciated. Thanks! :)