warning: multi-character character constant

Aug 23, 2010 at 1:03am
Hey im just trying to make a simple game to impress my dad because i want to but i get this error "warning: multi-character character constant". Any help? (btw: I need help stat)

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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main(){
	srand((unsigned)time(0));
	int random_integer;
	for(int index=0; index<20; index++){
		random_integer = (rand()%6)+1; 
	}
	int move;
	cout << "Would you like to go left, right, up, or down?";
	cin >> move;
	if (move = 'left'){
		if (random_integer > 4){
			cout << "You got a apple";
		}
		else{
			cout << "You found nothing";
		}
	}
	if (move = 'right'){
		if (random_integer > 4){
			cout << "You got a apple";
		}
		else{
			cout << "You found nothing";
		}
	}
	if (move = 'up'){
		if (random_integer > 4){
			cout << "You got a apple";
		}
		else{
			cout << "You found nothing";
		}
	}
	if (move = 'down'){
		if (random_integer > 4){
			cout << "You got a apple";
		}
		else{
			cout << "You found nothing";
		}
	}
}


Its a game where you go left, right, up, or down and if your lucky you get a apple. Could someone do a little clean up I think this thing will have some issues. I also wanna know if someone can happen to put in some code that allows me to type inventory and see how many apples I have.
Aug 23, 2010 at 1:11am
I don't know what you think if (move = 'left') is supposed to do. But it's not C++ ;o)

If you want to test if things are equal, you need to use double = like this if(a == b).

'left' is not a character nor is it a key code. You probably want to do something like if (move == 'l') and use the characters l, r, u, d for left, right, up, down respectively.
Aug 23, 2010 at 1:15am
Im a noob excuse my code. Thanks for that though.
Aug 23, 2010 at 1:19am
Ok now that error is gone a few new ones are here:

game.cpp: In function ‘int main()’:
game.cpp:16: error: expected primary-expression before ‘if’
game.cpp:16: error: expected ‘)’ before ‘if’
game.cpp:48: error: expected primary-expression before ‘}’ token
game.cpp:48: error: expected ‘;’ before ‘}’ token


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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main(){
	srand((unsigned)time(0));
	int random_integer;
	for(int index=0; index<20; index++){
		random_integer = (rand()%6)+1; 
	}
	int move;
	cout << "Would you like to go left, right, up, or down?(l,r,u,d)";
	cin >> move;
	if (if (move == 'l'){
		if (random_integer > 4){
			cout << "You got a apple";
		}
		else{
			cout << "You found nothing";
		}
	}
	if (if (move == 'r'){
		if (random_integer > 4){
			cout << "You got a apple";
		}
		else{
			cout << "You found nothing";
		}
	}
	if (move == 'u'){
		if (random_integer > 4){
			cout << "You got a apple";
		}
		else{
			cout << "You found nothing";
		}
	}
	if (move == 'd'){
		if (random_integer > 4){
			cout << "You got a apple";
		}
		else{
			cout << "You found nothing";
		}
	}
}


Oh and I wanna know if someone could put in that inventory suggestion i made that would be nice.
Last edited on Aug 23, 2010 at 1:20am
Aug 23, 2010 at 1:27am
What is if (if (move == 'l'){ supposed to be?? Change all of the if (if ( to if(
Aug 23, 2010 at 1:30am
>.< woops....
Aug 23, 2010 at 1:31am
Ok everythings fixed thanks but about that "inventory suggestion...
Aug 23, 2010 at 2:16am
For the inventory you could use a std::vector<> and every time you get an apple (or whatever) add it to the back of the vector. When the user types in 'i' you could list what is in the std::vector:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <vector>

// ... etc..

int main()
{
    // define your inventory as a vector of strings
    std::vector<std::string> inv;
    
    // ... stuff
    
    // Add an item to the inventory like this:
    inv.push_back("apple");

    // print out the inventory like this
    for(size_t i = 0; i < inv.size(); ++i)
    {
        std::cout << inv[i] << '\n';
    }
}
Last edited on Aug 23, 2010 at 2:16am
Aug 23, 2010 at 3:37am
Thanks.
Aug 23, 2010 at 3:47am
int is an integer. Unless the user inputs the numeric ASCII code for 'r' instead of the character 'r', this input isn't going to work.

If you want input as a character, then 'move' should be a char, not an int.
Topic archived. No new replies allowed.