Oh, oops. Sorry @ne555. I didn't see your response.
Thank you ne555 for responding!
I didn't notice the getline() before the loop, thanks!
- points, rebounds, assists, should be double instead of int
same for pnts, rbnds, assts, (¿are they charging you for vowels?) |
Ohh right, I totally overlooked the fact the input file was using doubles not ints... Whoops. Thank you both (ne555 & Andy) for pointing that out haha.
I took out the vowels because if I called them the same names as the member variables of the class, that doesn't mean I'd actually be using those variables, they'd just have the same name but different values which is confusing. Thus I named it something else, and I chose to take out the vowels because I felt that made more sense than any other name I could come up with.
- your "text" file was created in a different operating system than the one you are testing.
there the line end is marked with "\r\n" so you end up reading the '\r' character as a part of the string (use a debugger to watch your variables) |
I don't understand what you mean? I'm using Windows 10 with WSL (Windows Sub Linux) to use Ubuntu. What's that have to do with the .txt file though? Also, I've never seen an \r so I'm not sure what you mean.
----------------------------------
Thank you for your reply Andy!
Thanks for pointing out what ne555 said, I didn't see their post until now, oops.
I've made the changes you both mentioned but I'm still getting the same error for some reason.
Output:
1 2 3 4
|
Main.cpp: In function ‘int main()’:
Main.cpp:69:2: error: ‘readFile’ was not declared in this scope
readFile(Players);
^~~~~~~~
|
I originally used a header for the readFile() function because I know headers are inserted into the file when compiled so Main.cpp would have access to all functions in the header. I'm not as familiar with what happens when you put functions in a .cpp file and link them using the command:
g++ -std=c++14 Main.cpp MainFunctions.cpp Player.cpp
So I suspect the issue has something to do with that.
By the way, is it not possible to use my overload constructor instead of the line:
PlayersVec.emplace_back(pName, tName, pnts, rbnds, assts);
from MainFunctions.cpp? I'm still learning constructors/destructors so it'd be good practice to use the overload constructor but not required.
Lastly, is my destructor in Player.cpp correct? It's just empty like a tutorial I watched on YouTube:
1 2 3
|
Player::~Player() {
}
|
Main.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#include <iostream>
#include <vector>
#include "Player.h"
// All functions used in Main.cpp are in MainFunctions.cpp
int main() {
std::vector<Player> Players;
// Call function from MainFunctions.cpp
readFile(Players);
return 0;
}
|
MainFunctions.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 38 39 40 41 42 43 44 45 46 47
|
#include <iostream>
#include <string>
#include <vector>
#include <fstream> // file reading & writing
#include <stdlib.h> // for exit() & EXIT_FAILURE
#include "Player.h"
// Reads file, stores data into class, then closes file.
void readFile(std::vector<Player> &PlayersVec) {
std::ifstream iFile;
std::string fileName;
// Temp File Data Variables:
std::string pName, tName;
double pnts, rbnds, assts;
// Asks for user to input file name.
std::cout << "Please enter input file name: ";
std::cin >> fileName;
// Opens file name
iFile.open(fileName);
// If file cannot be opened, print error and exit.
if (!iFile) {
std::cerr << "Error! Could not open input file!\n";
exit(EXIT_FAILURE);
}
std::cout << "Successfully opened " << fileName << "!\n";
// Goes through every file line and stores info.
while (std::getline(iFile, pName)) {
std::getline(iFile, tName);
iFile >> pnts >> rbnds >> assts;
iFile.ignore(1000, '\n');
std::cout << pName << tName << pnts << rbnds << assts;
PlayersVec.emplace_back(pName, tName, pnts, rbnds, assts);
}
std::cout << "\n";
// Closes input file when done using it.
iFile.close();
}
|
Player.h:
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
// Player.h
#include <iostream>
#include <string>
// Guard:
// (Not compiler dependent guard type, that is good)
#ifndef PLAYER_H
#define PLAYER_H
class Player {
private:
std::string playerName;
std::string teamName;
double points;
double rebounds;
double assists;
public:
// default constructor
// sets all member variables to empty / zero.
Player();
// Overload constructor
Player(std::string, std::string, double, double, double);
// destructor
~Player();
// Accessor Functions
std::string getPName() const;
// getPName - returns name of player
std::string getTName() const;
// getTName - returns team name
double getPnts() const;
// getPnts - returns points
double getRbnds() const;
// getRbnds - returns rebounds
double getAssts() const;
// getAssts - returns assists
// Mutator Functions
void setPName(std::string);
// setPName - sets player name
// @param string - name of player
void setTName(std::string);
// setTName - sets team name
// @param string - name of team
void setPnts(double);
// setPnts - sets points
// @param double - points
void setRbnds(double);
// setRbnds - sets rebounds
// @param double - rebounds
void setAssts(double);
// setAssts - sets assists
// @param double - assists
|
Player.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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
#include "Player.h"
// Default constructor
Player::Player() {
playerName = "";
teamName = "";
points = 0;
rebounds = 0;
assists = 0;
}
// Overload constructor
Player::Player(std::string pName, std::string tName, double pnts, double rbnds, double assts) {
playerName = pName;
teamName = tName;
points = pnts;
rebounds = rbnds;
assists = assts;
}
// Destructor
// Should it be empty?????
Player::~Player() {
}
// Accessor Functions
std::string Player::getPName() const { return playerName; }
// getPName - returns player name
std::string Player::getTName() const { return teamName; }
// getTName - returns team name
double Player::getPnts() const { return points; }
// getPnts - returns points
double Player::getRbnds() const { return rebounds; }
// getRbnds - returns rebounds
double Player::getAssts() const { return assists; }
// getAssts - returns assists
// Mutator Functions
void Player::setPName(std::string pName) {
playerName = pName;
}
void Player::setTName(std::string tName) {
teamName = tName;
}
void Player::setPnts(double pnts) {
points = pnts;
}
void Player::setRbnds(double rbnds) {
rebounds = rbnds;
}
void Player::setAssts(double assts) {
assists = assts;
}
|
Thank you!