some errors running the program

I'm doing a Lab assignment for my C++ class, but I'm stuck on some errors which I can't understand. I need someone expertise here. Below is my code. I supposes to run them and give me the values of playername, playerid, currentlevel and topfivescore.

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
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <string>
using namespace std;


class GamePlayer { 

protected:
	char *playerName;
	char playerID[8];
	int currentLevel;
	int *topFiveScores[5];


public:
	GamePlayer () {
		playerID[8]=8;
		*topFiveScores[5]=5;
	} 

	GamePlayer (char *playerName1, char playerID1, int currentLevel1, int *topFiveScores1){
		
		playerID[8] = playerID1;
		currentLevel = currentLevel1;
		topFiveScores[5] = topFiveScores1;
		playerName = new char[strlen(playerName1) + 1]; 
		strcpy(playerName, playerName1);
	}

	~GamePlayer (){
	delete [] playerName;
	}

	void setplayerName ( char new_playerName) {
		*playerName = new_playerName;
	}
	void setplayerID (char new_playerID){
		playerID[8] = new_playerID;
	}
	void setcurrentLevel (int new_currentLevel){
		currentLevel = new_currentLevel;
	}
	void settopFiveScores (int new_topFiveScores){
		*topFiveScores[5] = new_topFiveScores;
	}


	char* getplayerName (){
		return playerName;
	}
	char getplayerID(){
		return playerID[8];
	}
	int getcurrentLevel(){
		return currentLevel;
	}
	int* gettopFiveScores(){
		return topFiveScores[5];
	}

};


int main (int argc, char* argv[]) {
	
	GamePlayer OGS;
	OGS.setplayerName ("abcd");
	OGS.setplayerID ("abcd001");
	OGS.setcurrentLevel (3);
	OGS.settopFiveScores (400);
	cout << "OGS name is " <<OGS.getplayerName <<endl;
	cout << "OGS player ID is " <<OGS.getplayerID <<endl;
	cout << "OGS current level is " <<OGS.getcurrentLevel() <<endl;
	cout << "OGS top score is " <<OGS.getcurrentLevel() <<endl;
	cin.ignore(2);
	return 0;
}


I don't know why it is giving me this error. Could someone please explain the problem, and give me some pointers as to how I might fix this?
closed account (o3hC5Di1)
Hi there,

You mention you are getting errors, but you didn't mention which errors you are getting?

Does the program compile? If not, which compilation errors do you get? Does the program crash, if so with which error? Are you expecting different output, if so, which output do you get and which do you expect?

Please let us know the errors you get and we'll be more equipped to help you.

All the best,
NwN
Hi,

These are the errors I am encountering.

warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

'GamePlayer::setplayerName' : cannot convert parameter 1 from 'const char [5]' to 'char'

'GamePlayer::setplayerID' : cannot convert parameter 1 from 'const char [9]' to 'char'

Any idea?
closed account (o3hC5Di1)
Hi there,

You declare those two functions as such:

1
2
void setplayerName ( char new_playerName);
void setplayerID (char new_playerID);


Char only holds one single character. What you mean to use is a char-array (also called a C-string):

1
2
void setplayerName ( char new_playerName[]);
void setplayerID (char new_playerID[]);


You will need to do the same for your constructor:

GamePlayer (char playerName1[], char playerID1[] //etc.)

Note that using std::string instead of char-arrays is a lot safer and recommended practice, but since it's an assignment you should probably use what you have learnt.

All the best,
NwN
Hi... Thanks for your help. I've edited the necessary parts and succeeded in debugging. However, when i run the program there is this message:

"Unhandled exception at 0x003b2dd6 in 2.exe: 0xC0000005: Access violation writing location 0xcccccccc."

It is a pop box message box either to click break or continue.

So i have really got no idea what's happening? any advice on this?
closed account (o3hC5Di1)
You are most likely experiencing the downside of using raw pointers and C-strings: segmentation faults.
These occur when your program tries to access memory that is outside the bounds of the memory provided to it by the operating system, or when you try to free unallocated memory.

If you have a debugger they can usually help you track these errors down. Clicking "break" may actually take you to a backtrace of what has happened. ANother way to figure this out is to put a std::cout statement after every line in main(), so you have an idea which function is causing the error.

Perhaps try that and get back to us with your revised code, so we can have a look.

All the best,
NwN
void setplayerName ( char new_playerName) {
*playerName = new_playerName; // wrong
}
void setplayerID (char new_playerID){
playerID[8] = new_playerID; // wrong
}
void setcurrentLevel (int new_currentLevel){
currentLevel = new_currentLevel;
}
void settopFiveScores (int new_topFiveScores){
*topFiveScores[5] = new_topFiveScores; // wrong
}


post these functions
Topic archived. No new replies allowed.