Begginer "if" problem

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# include <iostream>
#include<ctime>
#include<cstdlib>
#include<cctype>
using namespace std;
struct rooms
{
 char description[500];
 char name[32];
 int north;
 int south;
 int east;
 int west;
};
rooms room[11];
void Rooms();


 int main () {
cout << "\n\n\n"
	 << "======================================================\n"
     << "EarthWars Copyright Mark Larah --- marklarah@gmail.com\n"
     << "======================================================\n\n";
    
    cout << "Hello General. It is unfortunatley too late for the rest of them. There was nothing we could do. The people of Kai Feng have indeed been slaughtered heartlesly. We could not hold them off for long enough for our army to arrive. Oh well. Anyway, you have your army at your side, and the few people remaining in the village demand you avenge the death of your village. Go forth General, seek out the Pink Hand Rebel Forces, and eleimintae them. \n\nGood luck general!\n\n";
    string player;
	
	cout << "Sorry General, Im your Battle Coordinater, Toby. Im new here - I didn't catch your name? ";
	cin >> player;
	if (player == 'lol' or player == 'LOL'){
  cout << "\n Your name is lol? How interesting. I bet you really laughed out loud when entering your name. Anyway at anytime during the game (apart from battle), you can bring up a status report by pressing 'S'.";
}else if (player == 'dick' or player == 'dicks'){
  
	cout << "\n Your name is dick? How interesting. How incredibly mature. Anyway at anytime during the game (apart from battle), you can bring up a status report by pressing 'S'.";
}else if (player == 'poo' or player == 'ass'){
	cout << "\n That doesn't even desrve a witty easter egg remark. How incredibly mature. Anyway at anytime during the game (apart from battle), you can bring up a status report by pressing 'S'.";
   }else{
	cout << "\n Uh... of course, " << player << ". Anyway, hi. At anytime during the game (apart from battle), you can bring up a status report by pressing 'S'.";
	}
	cout << "\nAnyway, right now, were still in the village. Suggested possible routes we can take are:\n";
	
	return 0;

}


void Rooms()
{
	strcpy(room[0].description, "\nAnyway, right now, were still in the village. Suggested possible routes we can take are:\n");
 strcpy(room[0].name, "Village\n");
	room[0].north = -1;
	room[0].south = -1;
	room[0].east = 1;
	room[0].west = -1;
}


Im trying to make an Army RPG type thing, and im a beginner basically. I using Xcode as a compiler (im using Mac OS X, so its Unix, but at this stage it shouldn't really matter)

Anyway, on the ifs, for the name input, the error i get is like:


Warning: Multi Character Constant
Error! No match for 'operator==' in 'player==73697678'



Can anyone see my problem?
'player' is a variable of type string.
In C and C++:
1
2
3
"This is a string constant"  // which is the same as an array of char constant
'c'  // that is a char constant
'abcd'  // that is an integer, or "multi-character constant". 


[EDIT] THIS IS THE ANSWER: [/EDIT]
You want to check to see if your std::string has the same characters as a specified string constant:
if ((player == "lol") or (player == "LOL"))

Multi-character constants are typically used dangerously, so the compiler complains (Warning:) just to make sure you know that you are compiling potentially dangerous code. In your case, it is actually an error, since you shouldn't be using a multi-character constant.

C++ is very strongly typed, so the second line (Error!) is saying that it cannot find an overloaded == operator comparing a std::string and a multi-character constant, that is, an int (whose numeric value is 73697678, which is some name not listed in your code above).

Make sure to run your code through a spell-checker. I caught three, but I wasn't trying too hard.

You might also want to look up "word wrap" on Wikipedia for a simple line-break algorithm to fix those really long lines of text when output.

Hope this helps.
Last edited on
So what do i need to do?

have like

char[10] player;

or something?
I've already given you the answer.
there are to many variants of bad things that can be entered. i dont think you should pollute your code with it. i think you should just ignore it. if you want some kind of language filter, i think you should read in a file of possibilitys rather then hard code it. then you can add things to the file as needed without disrupting the code. i just think its low priority and you should concetrate on your story and stuff first. forget about trash talkers. its there loss.
Yes, but he's not filtering. He's making easter-eggs.

Though, I think I'd choose different words for the easter-eggs.

Personally, I'd use a map or lookup table of some sort and use the entered word as the key and the easter-egg text as the value. If the key is not in the table, then just output the default text.
Yeah. Thanks guys. I've fixed it now, but a new problem arises: I have a never ending loop:
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# include <iostream>
#include<ctime>
#include<cstdlib>
#include<cctype>
using namespace std;
struct rooms
{
 char description[500];
 char name[32];
 int north;
 int south;
 int east;
 int west;
};
struct avatar
{
 char name[32];
	int strength;
	int defence;
	int hp;
 int weapon;
 int armour;
};
void Rooms();
rooms room[11];

	char move;
	int hp=100;

	int curroom=0;
		
 int main () {
cout << "\n\n\n"
	 << "======================================================\n"
     << "EarthWars Copyright Mark Larah --- marklarah@gmail.com\n"
     << "======================================================\n\n";
    
    cout << "Hello General. It is unfortunatley too late for the rest of them. There was nothing we could do. The people of Kai Feng have indeed been slaughtered heartlesly. We could not hold them off for long enough for our army to arrive. Oh well. Anyway, you have your army at your side, and the few people remaining in the village demand you avenge the death of your village. Go forth General, seek out the Pink Hand Rebel Forces, and eleimintae them. \n\nGood luck general!\n\n";
    string player;
	
	cout << "Sorry General, Im your Battle Coordinater, Toby. Im new here - I didn't catch your name? ";
	cin >> player;
		cout << "\n Uh... of course, " << player << ". Anyway, hi. At anytime during the game (apart from battle), you can bring up a status update report by pressing 'U'. Now Lets go!";
		cout << "\n"
			 << "-----------\n"
		     << "EarthWars\n"
			 << "-----------\n\n";
	
					
		while (hp > 0){
		cout << room[curroom].description;
  if (room[curroom].north != -1){
  cout << "--(N)orth\n";
  }

if (room[curroom].east != -1){
  cout << "--(E)ast\n";
  }

if (room[curroom].south != -1){
  cout << "--(S)outh\n";
  }
  
if (room[curroom].west != -1){
  cout << "--(W)est\n";
  }
int d=0;
cout << room[curroom].name 
     << "----------\n"
	 << room[curroom].description;
cout << "Enter your move sir:\n ";
	while (d != 0){
	cin >> move;
	move = toupper(move);
switch (move) {
	case 'E':
 curroom = room[curroom].east;
 d=666;
		break;
	case 'W':
 curroom = room[curroom].west;
 d=666;
		break;
	case 'N':
	   curroom = room[curroom].north;
 d=666;	
			break;
	case 'S':
	    curroom = room[curroom].south;
		 d=666;	  

		break;
		case 'U':
	   curroom = room[curroom].south;
	    d=666;

		break;
	default:
		cout << "\nSorry sir, I don't understand, do you want to go N, E, S or W\n?" << endl;
	
  }
 }
}

return 0;


}
void Rooms()
{
	strcpy(room[0].description, "\nRight now, were still in the village. Suggested possible routes we can take are:\n");
 strcpy(room[0].name, "Village\n");
	room[0].north = -1;
	room[0].south = 6;
	room[0].east = 2;
	room[0].west = -1;
	}



Compile and see.

Can you see why?
Yes, your logic is messed up in two spots: the hp is never changed and d is never not zero.

It is difficult to read your code. Make sure you format it nicely. Also, here are a few suggestions.
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <iostream>
#include <ctime>
#include <string>
#include <vector>
#include <cctype>
using namespace std;

//----------------------------------------------------------------------
struct Room
{
	// use std::string for string data
	string description;
	string name;

	// the following list rooms adjacent to this room
	// if -1, then there is not any door in the indicated direction
	int north;
	int south;
	int east;
	int west;

	// Default constructor
	Room(): description(""), name(""), north(-1), south(-1), east(-1), west(-1) {};

	// A convenience constructor
	Room( string desc, string name, int n, int s, int e, int w ):
	  description(desc), name(name), north(n), south(s), east(e), west(w) {};

	// Display an individual room
	friend ostream& operator << (ostream&, Room&);
};

//----------------------------------------------------------------------
class Rooms: public vector<Room>
{
public:
	// The index of the room that the player is in
	int current;

	// The constructor. It will load the rooms from file, or whatever
	Rooms();
};

//----------------------------------------------------------------------
ostream& operator << ( ostream& outs, Room& room )
{
	// Display an individual room for the user, and his travel options.

	outs	<< room.description << '\n'
		<< "Suggested possible routes we can take are:\n";

	if (room.north != -1) outs << "--(N)orth\n";
	if (room.east  != -1) outs << "--(E)ast\n";
	if (room.south != -1) outs << "--(S)outh\n";
	if (room.west  != -1) outs << "--(W)est\n";

	return outs;
}

//----------------------------------------------------------------------
Rooms rooms;	// uses our fancy-pants constructor

int hp=100;	// this belongs to the avatar class

string player;	// this belongs to the avatar class also...
		// (since the avatar represents the player)

//----------------------------------------------------------------------
int main ()
{

	cout	<< "\n\n\n"
		<< "======================================================\n"
		<< "EarthWars Copyright Mark Larah --- marklarah@gmail.com\n"
		<< "======================================================\n\n";

	cout	<< "Hello General. It is unfortunatley too late for the rest of them. There was nothing we could do. The people of Kai Feng have indeed been slaughtered heartlesly. We could not hold them off for long enough for our army to arrive."
		<< "Oh well. Anyway, you have your army at your side, and the few people remaining in the village demand you avenge the death of your village. Go forth General, seek out the Pink Hand Rebel Forces, and eleimintae them. \n\nGood luck general!\n\n";
	
	cout	<< "Sorry General, Im your Battle Coordinater, Toby. Im new here - I didn't catch your name? ";

	// use getline when getting string data. See warnings below.
	getline( cin, player );

	cout	<< "\n Uh... of course, " << player << ". Anyway, hi. At anytime during the game (apart from battle), you can bring up a status update report by pressing 'U'. Now Lets go!";
	cout 	<< "\n"
		<< "-----------\n"
		<< "EarthWars\n"
		<< "-----------\n\n";
	

	// when HP reaches zero (or less) the player is dead					
	while (hp > 0)
	{
		// Tell the user where we are
		cout << '\n' << rooms[ rooms.current ];

		// Ask the user where to go
		int d=0;
		cout << "Enter your move sir: ";

		while (d == 0) {

			// Get the user's response
			char move;
			cin >> move;
			cin.ignore( numeric_limits<streamsize>::max(), '\n' );  // (the user pressed the ENTER: get rid if it)
			move = toupper(move);

			int curroom;  // where the user would like to go
			switch (move)
			{
				case 'E': curroom = rooms[rooms.current].east;  break;
				case 'W': curroom = rooms[rooms.current].west;  break;
				case 'N': curroom = rooms[rooms.current].north; break;
				case 'S': curroom = rooms[rooms.current].south; break;

				case 'Q': return 0;

				default:  curroom = -1;
			}

			// can we go where the user would like to go?
			if (curroom == -1)
			{
				// The response was an invalid direction.
				cout << "\nSorry sir, I don't understand, do you want to go N, E, S or W? " << endl;
			}
			else
			{
				// The response was valid.
				rooms.current = curroom;
				d=666;
			}
		}
	}

	return 0;
}

//----------------------------------------------------------------------
Rooms::Rooms(): current(0)
{
	// since we used strings, we can just assign using a string constant
	// how convenient

	// make sure to leave stuff like newlines and other formatting out
	// of the information. it is up to the thing printing the information
	// to decide how to format it.

	// of course, normally you'd read all this from file...
	// but for now a three-room maze will do

	push_back( Room(
		"Right now, were still in the village.",
		"Village",
		-1, 1, 2, -1
		) );
	push_back( Room(
		"You are on the road south of the village that leads to the forest.",
		"Forest road",
		0, -1, -1, -1
		) );
	push_back( Room(
		"You are in a grove east of the village.\n"
		"There are spooky lights in all around you.",
		"Magic Cove",
		-1, -1, -1, 0
		) );
}
OK, double-post since the last was so big.

Whenever you ask for input from the user, you can expect him to press the ENTER key. That puts a '\n' in the input stream.

getline() reads the '\n' and throws it away.
>> does not. So if you say
1
2
3
4
5
6
7
8
int my_int;
string my_string;

cout << "enter an integer: ";
cin >> my_int;

cout << "enter a string: ";
getline( cin, my_string );

your string will be the empty string and whatever the user typed for a string will still be unread by the input function, and will likely mess you up the next time you try to read something from the user.

The answer is, of course, to _always_ get (and throw away) that '\n'. Hence the cin.ignore() line.
1
2
3
4
5
6
7
8
9
int my_int;
string my_string;

cout << "enter an integer: ";
cin >> my_int;
cin.ignore( numeric_limits<streamsize>::max(), '\n' );

cout << "enter a string: ";
getline( cin, my_string );



The next thing is that you shouldn't use a standard array. Use a vector. Works much better. It can be inherited and given special powers (like I did in the example for the Rooms class).


Be careful to use good names. The variable 'd' is a bad name. What it does is force the user to enter a proper response. It might better be made a boolean value and named something like
bool isResponseOK;


I added a 'Q' response to the switch statement so that the program can be terminated by something other than Ctrl-C. I presume you will change that later.


A technical note: The last room added in the Rooms constructor uses string constant concatenation. That is:
string s = "String one" " another string?";
produces a single string. It is the same as:
string s = "String one another string?";
It is just convenient when you have really long strings you can break them across lines.

Hope this helps.
Last edited on
Wow! Thankyou soooo much! I have a couple of books, so i was just sticking to what i know. What I did was probably very simple (ie: wrong) :P

I shall now read up.

Thanks so much man.
Topic archived. No new replies allowed.