Array handeling

Ok so I am trying to manipulate an array of game charaters with this little function. Here are the problmes I am trying to set a name to the charater.with the void setName() function which I cant get to work. And also and the comipler says that
Error 3 error C3861: 'swapChar': identifier not found c:\users\kenshin\documents\visual studio 2008\projects\arraytest\arraytest\arraytest.cpp 69 ArrayTest

when I try to call the swapChar method. What am I doing wrong here?
Thanks :)
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
78
79
80
81
82
83
84
85
86
// ArrayTest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdafx.h"
#include <iostream>
#include <string>
#include <windows.h>
#include <cstdlib>
#include <ctime>

using namespace std;


//good guy ie player
class Player
{
	
	
public:
	int hp;
	int sp;
	int mp;
	void setName(String x);
			
};


int main()
{

	int i = 0; //set to defalut Main character Maverick

	//build player
	Player Maverick;
	Maverick.hp = 1220;
	Maverick.mp = 220;
	Maverick.sp = 100;
    
	Player Cap;
	Cap.hp = 50;
	Cap.mp = 100;
	Cap.sp = 155;

	Player Jen;
	Jen.hp = 1555;
	Jen.mp = 555;
	Jen.sp = 111;


	//array to hold characters
	//This is for characters 
	Player players[3];
	players[0] = Maverick;
	players[1] = Cap;
	players[2] = Jen;


	cout << players[i].name <<endl; //print name of charater

	cout << "Press s to change charater!" << endl;
	char button; //input for button pressed
	cin >> button;
	//switch function
	if(button == 's')
	{
		swapChar();
	}

	cout << players[i].name <<endl; //print name of charater

	return 0;
}


void swapChar()
	{
		
		
		if(i < 3)
		{
			i++; // i goes to 
			player[i];//switch to new charater
		}

	}
void swapChar() needs to be above the main. Order matters in C/C++, you have to declare a function (but don't have to define it) before you use it.
Also you're aware that you decalred void setName(String x) but didn't define it?
A lot of things are wrong.

First:

void setName(String x);

The parameter should be string x; case matters. Identifier name (used in lines 59 and 70) is not defined anywhere, also. I don't see a definition for setName() either.

Now to swapChar() and array manipulation. You need to declare a function before you use it. Either put the whole definition of swapChar() before main() or forward declare it. Even so, it shouldn't compile because you don't define what identifier i is. The int i defined in line 32 is local to main(). swapChar() can't know about it unless you pass it to it. To stay away from pointers for now, you should define swapChar() like this:

int swapChar(int i)

And store the value returned in main()'s i (you really should rename this to something more meaningful such as currentPlayer). Also, the swapChar() function needs to check whether you're trying to swap from the last element in the array to the first one, otherwise you'll eventually end up trying to access memory outside the array (player[3]).

Finally, note that this line

player[i];//switch to new charater

is completely useless. It simply returns player[i]'s value and does nothing to it. Semantically, it's as if you said

3;

I really recommend you substitute the array for a std::vector.
Last edited on
Thanks everyone I new to programming and trying to learn c++ on my own.
I will have to look into this vector thing if I have read it right vectors store objects right, I thought arrays did that too, am I wrong?
Internally, vectors store their objects in arrays. However, they can grow to accomodate more objects as needed, and they handle the lower level details of array manipulation for you. They also remember their own size. Using them is simpler and safer and allows you to work at a higher level of abstraction than arrays.
Topic archived. No new replies allowed.