C++ Text Adventure

I have a query
First Kindly read this code::

So I am building a c++ text adventure.

I have just started writing my code.

I want this story to have multiple endings

So if you read the last 5-6 lines of the code that are the 2 options that I gave

So if I create a if else statement such as if b==1 then say that the plane crashed.

This message( The plane crashed) will be shown to the user but the story will continue to move forward. But practically if the plane crashed the program should end.

In easy terms: If user selects the wrong option I want the code to end at that particular place and not go further

So what should I do in that code?

I want the code to stop after a particular if else statement

Return 0 from the main. This will say that the program wants to formally close.

1 Like

To built on what @toastedpitabread said:

In the event you needed to close a program from a function that is not in main, you can use the exit command included in <stdlib.h>

It takes an integer as an arguement, with 0 meaning that the program terminated correctly with no errors:

#include <iostream>
#include <stdlib.h>

int stop() {
  exit(0);
}

int main() {
  std::cin >> n;
  if (n == 0) {
    stop();
  } else {
    std::cout << "You did not click 0";
  }
}
2 Likes

Thanks Guys.I got the solution

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.