Read Function

I'm really having trouble finding a code to compile my program but here it goes.
All i need is to create a "read" function to read all the data for a Player Object.

So Far i made a display function to display all the data for the "Player" object, but i don't know how to make a read function.

Here is my program:

#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include "player.h"
#include "weapon.h"
#include "utilities.h"

using namespace std;

const int MIN = 1;
const int MAX = 100;

void displayPlayer(const vector<Player>& ePlayer);
void readPlayer(const vector<Player>&ePlayer, string pName, int ptype, int plevel, int pstrength, int wtype, int wdurability, int wlevRequired);

// main function ***********************************************************

int main()
{
vector <Player> gamePlayers;
string playerName;
int playerType, playerLevel, playerStregth, weaponType, weaponDurability, weaponlvlRequired;

readPlayer(gamePlayers, playerType, playerLevel, playerStregth, weaponType, weaponDurability, weaponlvlRequired);

displayPlayer(gamePlayers);

// End of program statements
cout << "Please press enter once or twice to continue...";
cin.ignore().get(); // hold console window open
return EXIT_SUCCESS; // successful termination

}

void readPlayer(vector<Player> ePlayer,string pName, int ptype, int plevel, int pstrength, int wtype, int wdurability, int wlevRequired)
{
for (int i = 0; i < 3; ++i)
{
cout << "Enter player name: ";
cin >> pName;
cout << "Enter player type (1, 2, 3, 4): ";
ptype = readValidInteger(1, 4);
cout << "Enter player level (>=1 && <= 100):";
plevel = readValidInteger(1, 100);
cout << "Enter player strength (>= 25):";
pstrength = readValidInteger(25, 200);
cout << "Enter weapon type (1, 2, 3, 4): ";
wtype = readValidInteger(1, 4);
cout << "Enter weapon durability (>= 25):";
wdurability = readValidInteger(25, 200);
cout << "Enter weapon level required (>=1 && <=100): ";
wlevRequired = readValidInteger(1, 100);

ePlayer.push_back(Player(pName, ptype, plevel, pstrength,
wtype, wdurability, wlevRequired));

cout << endl;
}
}


void displayPlayer(const vector<Player>& ePlayer)
{
for (int i = 0; i < ePlayer.size(); i++)
{
cout << left;
cout << setw(20) << "Player Name: " << setw(20)
<< ePlayer[i].getPlayerName() << endl;
cout << setw(20) << "Player Type: " << setw(10)
<< ePlayer[i].getType() << endl;
cout << setw(20) << "Player Level: " << setw(10)
<< ePlayer[i].getLevel() << endl;
cout << setw(20) << "Player Strength: " << setw(10)
<< ePlayer[i].getStrength() << endl;
cout << setw(20) << "Weapon Type: " << setw(10)
<< ePlayer[i].getWeaponType() << endl;
cout << setw(20) << "Weapon Durabiliry: " << setw(10)
<< ePlayer[i].getWeaponDurability() << endl;
cout << setw(20) << "Weapon Level Required: " << setw(10)
<< ePlayer[i].getWeaponLevelRequired() << endl << endl;
}
}
Hi,

"readPlayer" function you are using for filling the "gamePlayers", so we have to pass gamePlayers as reference to the function.

so please correct your read function as
void readPlayer(vector<Player>& ePlayer, string pName, int ptype, int plevel, int pstrength, int wtype, int wdurability, int wlevRequired);

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
void readPlayer(vector<Player>& ePlayer,string pName, int ptype, int plevel, int pstrength, int wtype, int wdurability, int wlevRequired)
{
for (int i = 0; i < 3; ++i)
{
cout << "Enter player name: ";
cin >> pName;
cout << "Enter player type (1, 2, 3, 4): ";
ptype = readValidInteger(1, 4);
cout << "Enter player level (>=1 && <= 100):";
plevel = readValidInteger(1, 100);
cout << "Enter player strength (>= 25):";
pstrength = readValidInteger(25, 200);
cout << "Enter weapon type (1, 2, 3, 4): ";
wtype = readValidInteger(1, 4);
cout << "Enter weapon durability (>= 25):";
wdurability = readValidInteger(25, 200);
cout << "Enter weapon level required (>=1 && <=100): ";
wlevRequired = readValidInteger(1, 100);

ePlayer.push_back(Player(pName, ptype, plevel, pstrength,
wtype, wdurability, wlevRequired));

cout << endl;
}
}


I feel this solve your problem, still if you have any problem please provide the details.
Last edited on
Thank you so much for answering, I appreciate your help.

Everything seems fine so far now that i corrected it but when i try to compile my program.I get that all my local variables are not being used and i get the error.

The ones such as:

string playerName;
int playerType, playerLevel, playerStregth, weaponType, weaponDurability, weaponlvlRequired;


Or is it that i'm doing it wrong when i call the readPlayer function?
readPlayer(gamePlayers, playerType, playerLevel, playerStregth, weaponType, weaponDurability, weaponlvlRequired);


int main()
{
vector <Player> gamePlayers;
string playerName;
int playerType, playerLevel, playerStregth, weaponType, weaponDurability, weaponlvlRequired;

readPlayer(gamePlayers, playerType, playerLevel, playerStregth, weaponType, weaponDurability, weaponlvlRequired);

displayPlayer(gamePlayers);

// End of program statements
cout << "Please press enter once or twice to continue...";
cin.ignore().get(); // hold console window open
return EXIT_SUCCESS; // successful termination

}

In main function flollowing veribles u r just declared (passing to the function as value type) but you are not using

1
2
string playerName;
int playerType, playerLevel, playerStregth, weaponType, weaponDurability, weaponlvlRequired;


so please remove these varibles from main function and declare inside the read funtion
so, your readPlayer funtion declaration looks like

void readPlayer(vector<Player>& ePlayer);

Definition of readPlayer funtion

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
void readPlayer(vector<Player>& ePlayer)
{
	string pName;
	int ptype, plevel, pstrength, wtype, wdurability, wlevRequired;

	for (int i = 0; i < 3; ++i)
	{
	  cout << "Enter player name: ";
	  cin >> pName;
	  cout << "Enter player type (1, 2, 3, 4): ";
	  ptype = readValidInteger(1, 4);
	  cout << "Enter player level (>=1 && <= 100):";
	  plevel = readValidInteger(1, 100);
	  cout << "Enter player strength (>= 25):";
	  pstrength = readValidInteger(25, 200);
	  cout << "Enter weapon type (1, 2, 3, 4): ";
	  wtype = readValidInteger(1, 4);
	  cout << "Enter weapon durability (>= 25):";
	  wdurability = readValidInteger(25, 200);
	  cout << "Enter weapon level required (>=1 && <=100): ";
	  wlevRequired = readValidInteger(1, 100);

	  ePlayer.push_back(Player(pName, ptype, plevel, pstrength,
	  wtype, wdurability, wlevRequired));

	  cout << endl;
	}
}


and your main function

1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
	vector <Player> gamePlayers;

	readPlayer(gamePlayers);

	displayPlayer(gamePlayers);

	// End of program statements
	cout << "Please press enter once or twice to continue...";
	cin.ignore().get(); // hold console window open
	return EXIT_SUCCESS; // successful termination
}


Let me know if you have any issues.
Last edited on
Topic archived. No new replies allowed.