Matt Bettinson
Matt Bettinson

Reputation: 531

How do I call up a function from anywhere in the program in C++?

Here is the code:

while (inDungeonRoom1 == true) {

    if (choice == "torch" || choice == "Torch" || choice == "pick up torch" ||
      choice == "Pick up torch" || choice == "grab torch" ||
       choice == "Grab torch") {

          torch++;

I want to be able to call a function, like information about where you are anywhere within the program, without messing with if statements.

Upvotes: 0

Views: 577

Answers (2)

Mac
Mac

Reputation: 14791

You can declare your function as static (if a member function), or as quasiverse mentioned simply declare it above where you want to use it. Then, you can simply call it as needed.

Also important is that comparing strings using == is really not a good idea. If you're using C-strings, you'll want to look into using strcmp, or if you're using C++ strings it's worth looking at string::compare.

EDIT: updated wording a little in response to Sergey's comment.

Upvotes: 2

zwol
zwol

Reputation: 140788

You appear to be programming an interactive fiction game. You want a more structured way of interpreting player input -- in other words, a parser. Natural language parsing is obscenely difficult, but there are some good-enough-for-this-job parsers that you could reuse. I recommend you look through the IFwiki's guide to authoring systems.

Upvotes: 5

Related Questions