Cannot add two pointers?

Hello again,

In my text-based RPG, I am currently developing the (simple) combat system. However, I have run into some trouble along the way. In this section of the code, when the player presses 'a' or 'A', the enemy's (goblin's) HP is decreased by a random value near the playerDMG value. However, that is not where the problem lies. After this "attack" has occurred, and the goblin's HP has been subtracted, I set a string (actionMSG) which displays in the HUD at the start of the main loop. Here is where I hit a roadblock. I want the message to be, for example, "Normal Attack! - 49 DMG!", but I get the error, "1>c:\documents and settings\*********\desktop\other\coding\rpg_combat\rpg_combat\main.cpp(70) : error C2110: '+' : cannot add two pointers".

Here is 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
		//display HUD
		cout << "Player             | Computer     |" << endl;
		cout << "HP: " << playerHP_GUI[playerHP] << "     | HP: " << goblinHP << "       |" << endl;
		cout << "Level: " << playerLVL << "  EXP: " << playerEXP << "   |              |" << endl;
		cout << "-------------------+--------------+" << endl;
		cout << "Commands           |"<<endl;
		cout << "A: Normal Attack   |"<<endl;
		cout << "D: Use Potion (" << playerPOT << ")  |" <<endl;
		cout << "Q: Quit            |"<<endl;
		cout << "-------------------+"<<endl<<endl;
		cout << actionMSG; //<-- display last action 
		actionMSG="";
		//get input
		playerACT = getch();
		switch( playerACT ) {
			case 'a':
			case 'A':
				int dmgDealt = rand() % (playerDMG+5) + (playerDMG-5);
				goblinHP = goblinHP - dmgDealt;
				actionMSG = "Normal Attack! - " + dmgDealt + " DMG!"; //<-- error on this line <--
				break;
			default:
				break;
		}


So my question is this: which operator do I use to accomplish this?
Last edited on
You can either do it this way:

actionMSG = string("Normal Attack! - ") + dmgDealt + " DMG!";

or

1
2
actionMsg = "Normal Attack! - ";
actionMsg.append(dmgDealt + " DMG!");


The reason is because the system does not have a way to add a const char* and a string ("stuff" + string), but string has a way to add a string and a const char* (string + "stuff").
Topic archived. No new replies allowed.