Hey, I'm very new to c++ and programming in general, so apologies if the answer is obvious. I decided to make a micro text-rpg to familiarise myself with the syntax, and I have encountered a problem when creating the basic shell of the game. At each area, the player states where to go. So if they wanted to go to the jungle, they would type Jungle. So in the main function, each function is called upon with something like:
if ( choice == Clearing )
Clearing();
So, as you can probably tell, this will not work. (Error message ISO C++ forbids comparison between pointer and integer)
Also, in a few areas, the use has no choice about where to go. This also will not work.
int Cliff()
{ cout << "You arrived at a cliff." << endl;
cin.get();
cout << "There is no way forward. Press enter to go back" << endl;
cin.get();
choice == Lake;
}
(Same error message)
So what I was thinking, is do I have to reformat the game, or is there an easy way to get C++ to read the user input and use it as data? At the minute I am thinking I will have to make each choice a number rather than a word, but I know nothing.
It's hard to tell from the code samples shown, but I get the feeling that working though the tutorial from the start might be very useful. http://www.cplusplus.com/doc/tutorial/
There are a couple of problems with the snippet you posted.
1 2
if ( choice == Clearing )
Clearing();
1) The error message says choice was declared as an int. Is the user entering a number or string (characters)? If you want the user to enter characters, then inputting to an integer is not what you want.
2) If you're meaning to compare character strings, then the use of Clearing in the if statement must be quoted.
Consider the following instead:
1 2 3 4 5
string choice;
cin >> choice;
if (choice == "clearing") // assumes the user enters all lower case
Clearing();
Not sure what your issue is with the cliff. You've told the user he can't go forward.
You're going to need some logic to make sure the user doesn't go forward. i.e. Etiher forward is not a valid move, or the user falls off and kills himself.
BTW, choice == Lake; is not a useful statement. If you meant an assignment, then you need to use a single =. A double equal is a comparison. Also, what is the type of Lake? Is it an enum, a function or did you mean "lake"?
for(i=0; i<3; i++)
if (options[i].compare(choice)==0) break;
switch(i) {
case 0:
cout << "go to the Jungle";
break;
case 1:
cout << "welcome to the Lake";
break;
case 2:
cout << "watch what a beatiful beach";
break;
default:
cout << "wrong choice";
}
Okay thanks for all the help guys, I used most of the fixes listed here, and here is a snippet of the fixed code.
1 2 3 4 5 6 7 8 9
int Village()
{ cout << "You arrived at a village.";
cin.get();
cout << "You can go to the castle, or back to the clearing.";
cin.get();
cout << "State the name of the place you wish to go to. ENTER: ";
cin >> choice;
}
//normal area
1 2 3 4 5 6 7
int Cliff()
{ cout << "You arrived at a cliff.";
cin.get();
cout << "There is no way forward. Press enter to go back";
cin.get();
choice = "lake";
// no way forward area
and in main(),
1 2 3
if ( choice == "clearing" )
Clearing()
//so if the user enters that, they go there
What I may change in the next version of this, is making it multiples choice rather than asking the user to type in where to go.
Also I may add in aspects such as adding bools to only allow the user into certain areas if they have done certain things.