text based rpg error

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
#include <iostream.h>
#include <time.h>
#include <stdlib.h>
void main(void)
{
	srand(unsigned(time(NULL)));
	int dice;
	float playerhealth;
	playerhealth=100;
	int movement;
	movement=0;
	int keyboard_check; 
	int gold;
	gold=0;
	char inventory1[]="Empty";
	char inventory2[]="Empty";
	char inventory3[]="Empty";
	while (playerhealth>=0)
	{
	cout << "\nPress '1' to roll the die. "; 
	cout << "\nPress '2' to view your inventory. ";
	cout << "\nPress '3' to visit the shop. ";
	cin >> keyboard_check;
	if (keyboard_check==1)
	{
		movement=movement++;
		dice=rand() % 2;
		if (dice==1)
		{
			cout << "\nYou have been attacked by an enemy!";
			playerhealth=playerhealth - rand() % 6;
			cout << "\nPlayer's health is: " << playerhealth;
			if (playerhealth==0)
			{
                cout << "\nYou are dead!";
				cout << "\nYou finished the game with: " << gold << "gold";
			}
		}
		if (dice==0)
		{
			cout << "\nYou found a pot of gold!";
			gold=gold + rand() % 11;
			cout << "\nGold: " << gold;
		}
	} 
	if (keyboard_check==2)
	{
        cout << "\n" << inventory1;
		cout << "\n" << inventory2;
		cout << "\n" << inventory3;
	}
	if (keyboard_check==3)
	{
	   cout << "\nPress '1' to buy a potion for 100 gold. ";
	   cin >> keyboard_check;
	   if (keyboard_check==1)
	   {
	   gold =gold;
	   inventory1[]="poiton";
	   }
	}
	}
}


In this code I get this error message "--------------------Configuration: Text Based RPG - Win32 Debug--------------------
Compiling...
Text Based RPG.cpp
C:\Documents and Settings\Admin\Desktop\Text Based RPG\Text Based RPG.cpp(59) : error C2059: syntax error : ']'
Error executing cl.exe.

Text Based RPG.exe - 1 error(s), 0 warning(s)"

Can someone please help? I am a beginner with C++, but I have used other very basic programming languages.
???

inventory1[]="poiton";

Which element of the array are trying to insert the string into?

(Had you already had a really good look at line 59?)

Andy
I am not quite sure on what I am doing with arrays i guess. I am going to re-read the chapter on arrays in the text book I've been using.
I thought with the array the way I'm using it, the number in the [] referred to the number of characters in the value. I'm kinda confused.
Last edited on
When you declare an array, the value in the [] its size

When you access an element, it's the index of the element.

Remember that C/C++ arrays are indexed from zero!

Andy
Thank you
Topic archived. No new replies allowed.