Text Adventure Confusion?

closed account (j1CpDjzh)
I tried creating a function that checks input so that I wouldn't have to write a billion if statements, however I don't quite understand how I'd plug the next part of the story into the function.

Here's the function

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
void inputChecker1(string userinput, string answer1, string answer2)
{
    getline(cin, userinput);
    makeUppercase(userinput);
    if (userinput == answer1)
    {
        //Continue story
    }
    else if (userinput == answer2)
    {
        //Continue story
    }
    else if (userinput == "MENU")
    {
        menu();
    }
    else if (userinput == "EXIT")
    {
        exitGame();
    }
    else
    {
        errorMessage();
    }
}


Should I turn this function into a string that returns the userinput, and make another function that checks it's return output? That way, based on what's returned it could print out the next part of the story? How exactly would I be able to achieve this because I'm confused. Is there an easier way to do this?

I'm sorry if my question is hard to understand, I'm not very good at explaining things.
Last edited on
I think you can call whatever you want to do next from this function - like showing story etc.

In case you use the return value of this function in the called function, you will probably need to use a switch in the called function too, to decide on the action. This will make the code redundant.

I don't understand why you have to pass answer1 and answer2 to the function, in my opinion that would be something defined?

or if you want to set functionChecker is the only function to check all way-selector in the game it should be,
1
2
3
void inputChecker(int presentState,string userinput,string answer1,string answer2,int nextState1,int nextState2){
    ...state selector...
}


and you have to make state is a list of story-board, defined by number. Maybe detail stored in arrays or files.
like
1: begin : yes->2, no->3
2: select mom : male -> 4,female->5
3: go to hell
4: happy
5: select dad ...
...

something like that?
Last edited on
closed account (j1CpDjzh)
Well, answer1 and answer2 would be defined in main() (or as I did it void game() ((which gets called in main after a start menu)) ) as something like
1
2
3
4
string userinput1;
    string answerChoice1 = "Y";
    string answerChoice2 = "N";
    inputChecker1(userinput1, answerChoice1, answerChoice2);

I like the story-board idea a lot though, and I'm going to try it out. I think what I'm going to do is once it passes through the if statement, a number is a signed to an int. The int is then checked for by a switch statement, and will print the next part of the story based on the number... Or is that too complicated, and would it be easier with arrays or files? I'm still learning about files, but so far they don't seem too complex, so I might try that out.

Thank you both so much for your help!

EDIT: I now realize that my way of trying to do it is way complicated.

EDIT 2: I've decided that the input checker is only going to check for the words "MENU", "EXIT", and generate an error message when userinput != answer1 & userinput != answer2
Last edited on
It's not much complicated about coding.
For my experience in coding game (especially in Assembly), the story-order-in-line and story-special-action-to-user is much harder.

(story-order-in-line is just like my last example, if you have 100 state maybe state 50 have to direct to state 99 and 100 will make you confuse if you decided it not good enough.)

How to response to player is what you have to decide in separated function.
So function state-selector is just function used for choose the way game will go on, and which function will be used to response.
Hardcoding design problem. A common mistake beginners make.

I outline alternative approaches in this thread:

http://www.cplusplus.com/forum/beginner/71141/#msg379639



EDIT: Or did I totally misunderstand this thread? That's what I get for skimming. Nevermind. =X
Last edited on
closed account (j1CpDjzh)
Ohh, I see what your saying now.

So I'll make a function with all the story lines, and the selector to choose a storyline based on provided input. That sounds a lot easier than what I was trying to do, thank you so much!

By the way, thanks Disch. That links kind of provides me of a better understanding of how files would work in this situation.

I'm going to go try all of this out and I'll let you know how it goes.
Last edited on
Topic archived. No new replies allowed.