Adding a C++ Class and File I/O in VS Community

This is a rough draft of my eventual page. I will be adding a video on how to create a class in Visual Studio Community C++ and write a File Input/Output file connected to Players and a Player class. The example already has the FileIO class already fairly complete and starts by creating a Player class.

Main Test programs

To test the classes, a test file must be written. To have it executed, I put it in the main() function. You can see it is just a series of calls to member methods for each class.

/* Main.cpp
 * Wayne Cook
 * 8 December 2020
 * Test Program that exercises the class member methods.
 */

#include "FileIO.h"
#include "Player.h"

int main() {
	FileIO fileIO("minesweeper");
	Player bob = fileIO.findPlayer("Bob");
	Player george = fileIO.findPlayer("George");
	Player fred = fileIO.findPlayer("Fred");
	cout << "First game." << endl;
	fileIO.updatePlayer(bob);
	fileIO.updatePlayer(george);
	fileIO.printPlayers();
	cout << "Next two games." << endl;
	george.incrementLosses();
	bob.incrementWins();
	bob.incrementTies();
	fred.incrementTies();
	fileIO.updatePlayer(bob);
	fileIO.updatePlayer(george);
	fileIO.updatePlayer(fred);
	fileIO.printPlayers();
	return 0;
}

FileIO Class

This was written to read from and write to a specified file the data for each of the players who have played a game.

Header File (.h)

#pragma once
/* FileIO.h
 * Wayne Cook
 * 8 December 2020
 * This is a sample File I/O file that may be helpful in your project.
 */

#ifndef _FILEIO_
#define _FILEIO_

#include <string>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

#include "Player.h"										// Local player class

class FileIO
{
private:
	string fileBase,									// User set base
		   mainFile,									// Main text file
		   backFile;									// Backup text file
	vector<Player> playerList;							// Keep a list of players
public:
	FileIO(string = "scores");							// Constructor
	~FileIO();											// Destructor

	/* Player findPlayer(string);
	 * Given a name, find the player's statistics
	 * Input: Player Name
	 * Return: Player statistics from file, if no entry, returns all zeros.
	 */
	Player findPlayer(string);

	/* Player void updatePlayer(Player);
	 * Add/Update a player to/in the internal vector.
	 * Input: Player information
	 * Return: Nothing.
	 */
	void updatePlayer(Player);

	/* void printPlayers();
	 * Prints list of Players stored in the vector.
	 * Input: Player information
	 * Return: Nothing.
	 */
	void printPlayers();
};
#endif

Code (cpp File)

/* FileIO.cpp
 * Wayne Cook
 * 8 December 2020
 * This is a sample File I/O file that may be helpful in your project.
 */

#include "FileIO.h"

// Constructor - Read the file if it exists.
FileIO::FileIO(string name) {
	ifstream input;
	Player player("player");
	fileBase = name;
	mainFile = fileBase + ".txt";
	backFile = fileBase + ".bak";
	input.open(mainFile);							// Open the users scores file
	if (input) {									// Check if file exists
		ofstream output;
		output.open(backFile);
		playerList.clear();							// Make sure the list is empty
		while (input >> player.name >> player.games >> player.wins
			>> player.losses >> player.ties)
		{
			playerList.push_back(player);			// Put all names into a vector
			// And into a back up file.
			output << player.name << ' ' << player.games << ' ' << player.wins << ' ' <<
				player.losses << ' ' << player.ties << endl;
		}
	}
}

// Destructor - Write the vector back to the file.
FileIO::~FileIO() {
	ofstream output;
	Player player("player");
	output.open(mainFile);
	for (int i = 0; i < (int)playerList.size(); i++) {
		player = playerList[i];
		output << player.name << ' ' << player.games << ' ' << player.wins << ' ' <<
			player.losses << ' ' << player.ties << endl;
	}
}

/* Player findPlayer(string);
 * Given a name, find the player's statistics
 * Input: Player Name
 * Return: Player statistics from file, if no entry, returns all zeros.
 */
Player FileIO::findPlayer(string n) {
	Player retVal(n);
	for (int i = 0; i < (int)playerList.size(); i++) {
		if (n == playerList[i].name) retVal = playerList[i];
	}
	return retVal;
}

/* Player void updatePlayer(Player p);
 * Add/Update a player to/in the internal vector.
 * Input: Player information
 * Return: Nothing.
 */
void FileIO::updatePlayer(Player p) {
	bool playerFound = false;
	for (int i = 0; i < (int)playerList.size(); i++) {
		if (p.name == playerList[i].name) {
			playerList[i] = p;
			playerFound = true;
		}
	}
	if (!playerFound) playerList.push_back(p);		// Put the new player into a vector
}

/* void printPlayers();
 * Prints list of Players stored in the vector.
 * Input: Player information
 * Return: Nothing.
 */
void FileIO::printPlayers() {
	Player player("player");
	for (int i = 0; i < (int)playerList.size(); i++) {
		player = playerList[i];
		cout << player.name << ' ' << player.games << ' ' << player.wins << ' ' <<
			player.losses << ' ' << player.ties << endl;
	}
}

Player Class

This set of file provides the interface to the player methods and attributes. It assumes the player wants to track games, wins, losses, and ties.

Header File (.h)

#pragma once
/* Player.h
 * Wayne Cook
 * 8 December 2020
 * This is a sample Player file that may be helpful in your project.
 */

#ifndef _PLAYER_
#define _PLAYER_

#include <string>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std; class Player
{
public:
	string name;									// Players name
	int games,										// Number of games played
		wins,
		losses,
		ties;
	Player(string, int = 0, int = 0, int = 0, int = 0);
	~Player();

	inline void incrementWins() {
		wins++;
		games++;
	}

	inline void incrementLosses() {
		losses++;
		games++;
	}

	inline void incrementTies() {
		ties++;
		games++;
	}

	/* void operator=(Player)
	 * Be able to set two players to have equal valued
	 * Input Another Player
	 * Set values of current player to the passed in player
	 */
	void operator=(Player);
};
#endif

Code (cpp File)

#include "Player.h"

Player::Player(string n, int g, int w, int l, int t) {
	name = n;
	games = g;
	wins = w;
	losses = l;
	ties = t;
}

Player::~Player() {}

/* void operator=(Player)
	 * Be able to set two players to have equal valued
	 * Input Another Player
	 * Set values of current player to the passed in player
	 */
void Player::operator=(Player p) {
	name = p.name;
	games = p.games;
	wins = p.wins;
	losses = p.losses;
	ties = p.ties;
}