Problem with user input?

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.
Clearing() is a function.
Clearing (without parenthesis) is pointer to function.
choise is an integer.

cin >> clearing;
(note c lowercase) read from keyboard and copy in clearing

strcmp(choise, clearing) compare string "choise" with string "clearing"
(strcmp return 0 if choise==clearing):

if(!strcmp(choise,clearing) Clearing();

declare "char choise[any number]" and assign string values.
A few things:

Why do I have to do cin >> clearing rather than declare the string as clearing?

And does the second bit mean that I have to make each option a string and then compare each string to the string "choice"?

And finally, you said declare char choice, but isn't char previously a string?
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/
Would it help if I posted a larger sample?
And have looked through tutorial before, not sure if I have found a way to do what I want though
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"?
Last edited on
this Error message

"ISO C++ forbids comparison between pointer and integer"

means:
choice is an integer and Clearing is pointer to function Clearing().

I have made a mistake in "cin >> clearing". It was cin >> choice.

watch this example using class string:
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[]) {
int i;
string choice;
string options[] = {
"Jungle",
"Lake",
"Beach"
};
for(int i=0; i<3; i++) cout << options[i] << endl;
cout << "Choice: ";
cin >> choice;

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";
}

system("PAUSE");
return EXIT_SUCCESS;
}

Last edited on
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.

Many thanks guys :)
Topic archived. No new replies allowed.