Strings and Char Arrays

I am pretty new to C++, and am having some trouble with Char arrays. Heres my code:
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
#include "stdafx.h" //includes all my headers

using namespace std;

class Player_Setup{
private:
	char pname[15]; //this is the char array
	int crossheir_min;
	int crossheir_max;
	int crossheirs_type;
	int x, y, z;
	float xzdeg;
	float ydeg;

public:
	Player_Setup()
		{pname = "no name\0";} // '\0' isn't working

	Player_Setup(char name[15])
		{pname = name;}

	char[15] Player_Name() // i don't know how to return an array
		{return pname;}
	
	int Crossheir_Size(bool type)
		{if (type == 0) return crossheir_min;
		else return crossheir_max;}

	void Change_Crossheir_Type(int type)
		{crossheirs_type = type;}

	int Crossheir_Type(){return crossheirs_type;}

	void Change_Name(char name[15]) //this wont work
		{pname = name;}

	void Display_Name()
		{cout << pname;} //this is all that works
};


the problems are that the functions wont work. can any one help?

Read this: http://cplusplus.com/doc/tutorial/ntcs/

Then try and fix the code and if you still get into problems then ask. :)
I got 3 errors. 1st was: "error C2440: '=' : cannot convert from 'const char [15]' to 'char [15]'" how do i fix that? the other two were both "error C2440: '=' : cannot convert from 'char []' to 'char [15]'"
void Change_Name(char name[15]) //this wont work


Change it to:

void Change_Name(char name[ ])

And change

 
{pname = "no name\0";} // '\0' isn't working 


to:
1
2
3
4
{
memset(pname,NULL,sizeof(pname)); 
strcpy(pname,"no name");
} // '\0' isn't working 


Hope this helps !
Last edited on
Since you are using a C++ class, why not just use std::string?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <string>
using namespace std;

class Player_Setup{                
private:
	string pname;
	int crossheir_min;
	etc

public:
	Player_Setup():
		pname( "no name" )
		{ }

	etc
};

Also, using the #include <stdafx.h> is a kludgy Microsoft thing. You should not rely on it. You are better off just listing the exact headers you need. That way you won't be surprised or annoyed later on when you make changes to any part of your program or compile environment.

Hope this helps.
Last edited on
Topic archived. No new replies allowed.