Errors in Code - Structures & arrays

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 foroperator[]’ (operand types are ‘playerData’ and ‘int’)
         std::getline(playersFile, playerList[i].pName);
                                             ^
nba.cpp:115:45: error: no match foroperator[]’ (operand types are ‘playerData’ and ‘int’)
         std::getline(playersFile, playerList[i].tName);
                                             ^
nba.cpp:116:31: error: no match foroperator[]’ (operand types are ‘playerData’ and ‘int’)
         std::cin >> playerList[i].points;
                               ^
nba.cpp:117:31: error: no match foroperator[]’ (operand types are ‘playerData’ and ‘int’)
         std::cin >> playerList[i].rebounds;
                               ^
nba.cpp:118:31: error: no match foroperator[]’ (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! :)
Last edited on
nba.cpp:95:5: error: ‘readPData’ was not declared in this scope

Line 18: readPData is a member function of your struct. readPData really doesn't belong as a member function. i.e. It's not a function related to a single player. Move line 18 to line 5 so that it becomes a function declaration.

nba.cpp:114:45: error: no match for ‘operator[]’ (operand types are ‘playerData’ and ‘int’)

Line 26: playerlist is not passed as an array.

Lines 31-35: It's not a good idea to switch between getline() and the extraction operator >>.
The extraction operator>> leaves the new line character in the stream which getline() will retrieve it and then consider the operation complete.
So what you need to do is to consume that new line character before you try to retrieve the string.
One way is to use the istream.ignore() function to empty the input buffer.
See http://www.cplusplus.com/reference/istream/istream/ignore/ for more information.

======================================
Update: Figured it out with the help of someone in my class.
All fixed! :)
======================================
@AbstractionAnon
Ah okay thanks! I forgot about cin.ignore(). Although, I'm not sure how to use it for a file. I tried this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    for (int i = 0; playersFile.eof() == false; i++) {
        std::getline(playersFile, playerList[i].pName); // will check file line and assign the line contents to playerList[i].pName
        std::getline(playersFile, playerList[i].tName);
        
        // .ignore() will ignore the '\n' that come after getline()
        playersFile.ignore(1000, '\n');
        playersFile >> playerList[i].points;
        playersFile >> playerList[i].rebounds;
        playersFile >> playerList[i].assists;
        
        std::cout << playerList[i].pName << "\n";
        std::cout << playerList[i].tName << "\n";
        std::cout << playerList[i].points << "\n";
        std::cout << playerList[i].rebounds << "\n";
        std::cout << playerList[i].assists << "\n\n\n";
        
    }


Which didn't give any compile errors. However, when I tested the code it said a TON of numbers like:
6.95334e-310
4.94066e-324
6.95334e-310

Then said "Segmentation fault (core dumped)" which has something to do with memory.

What am I doing wrong?

Thanks!
======================================
Update: Figured it out with the help of someone in my class.
All fixed! :)
======================================

Last edited on
Topic archived. No new replies allowed.