Jump to content
  • Advertisement
Sign in to follow this  
desballs

i need to link up condetional statements

This topic is 3411 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

ok I'm making a text based game with conditional statements and don't know how to link them together so that when you answer one question it gives you the next pleas help post examples of how it works if you can

Share this post


Link to post
Share on other sites
Advertisement
Why not post what you have working? This way we can tell what is causing you trouble.

Share this post


Link to post
Share on other sites
I would turn every question into a function. The function prints the question text, parses the user's answer, and then specifies the next question to be asked. This can happen in several ways but will probably happen either by returning the next question (a function) or by setting a "next question" variable through a side-effect. For instance:


class Questions
{
void (Questions::*next_question)();

int Ask(const std::string &ask, int count)
{
int choice = 0;
do
{
std::cin.clear();
std::cin.ignore();
std::cout << ask;
}
while (! (std::cin >> choice) && choice > 0 && choice <= count);
}

void Welcome()
{
int choice =
Ask("Welcome! Go which way?\n 1. North\n 2. South\n\n", 2);
if (choice == 1) next_question = &Questions::North;
if (choice == 2) next_question = &Questions::South;
}

void North() { .. }
void South() { .. }

public:

Questions() : next_question(&Questions::Welcome) {}

bool Step()
{
if (next_question) { this->*next_question(); }
return next_question;
}
};

Share this post


Link to post
Share on other sites
ok well i have a very basic idea of it but its basically all going to be Q&A i don't know how to link together Q&A's but i know how to write one

Share this post


Link to post
Share on other sites
You mean something like this ;

cout << " Welcome, do you want to play (y/n) ? : ;
char play = 'y';
cin >> play;

while(play == 'y')
{
//run the game;

cout<<You died. Do you want to play again ? ;
cin >> play;

//If yes restart the game.

}


return 0;

}


Share this post


Link to post
Share on other sites
Quote:
Original post by desballs
ya some of it will be y/n but some of it will be 3 choices


ToohrVyk's program includes an example of a function that gives you N choices:

int Ask(const std::string &ask, int count)
{
int choice = 0;
do
{
std::cin.clear();
std::cin.ignore();
std::cout << ask;
}
while (! (std::cin >> choice) && choice > 0 && choice <= count);
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!