If statememt true, how to stop program without restarting?

Its a simple text based game. Program runs fine. but I want it so when my if statement is true, it stops the program but doesn't restart it.

1
2
3
4
5
6
7
8
9
 		cout << "Which do you choose?\n\n";
		
		cout << "Left or Right\n\n";
		
		cin >> Direction;
		
		if ( Direction == "Left" ) {
			cout << "\nYou walk through the brightly lit path to an opening, the\nsun is shining so bright through the opening you can barely see. You step off a cliff and fall to your death.\n\n";
			cout << "GAME OVER"; }
Last edited on
So I need a code that If statement is true it stops the program after the cout game over.
Currently my if statement would equal true then game over but it will still continue the game to the next cout.
We can't really answer the question without seeing the rest of your code. Is that code inside main?

If so, you could return 0 after that cout game over statement (but still inside that if statement).
okay I will try that and yes its all inside main. So I can return 0; within if statements { }? And if else should proceed instead it will continue the program?
Yes, you can have return statements in if statements.

Perhaps a cleaner way to do it is to make all your other code occur within the else statement, and return 0; is right after that.

So for example...
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
    if (Direction == "Left") {
        cout << "GAME OVER";
    }
    else
    {
        // All other stuff goes here
    }

    return 0;
}


But as I said... it's hard to suggest a good approach without seeing what the rest of your code is doing.
Last edited on
its just simple cout questions and cin answers stored in string. But your advice worked out perfectly. I aprechiate it. here's the code so far.

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <string>

using namespace std;

int main ( ) 
	{
		string name;
		string gender;
		string boy;
		string girl;
		string race;
		string Human;
		string Orc;
		string Elf;
		string Direction;
		
		cout << "Hello, welcome to the World.\n\n";
		
		cout << "What's your name?\n\n";
		
		cin >> name;
		
		cout << "\nNice to meet you " << name << endl << endl;
		
		cout << "Are you a boy or girl?\n\n";
		
		cin >> gender;
		
			if ( gender == "boy" ) 
			{
				cout << "\nKind of small for a boy, hope you learn fast.\n\n";
			}
				else 
					{ cout << "\nA girl? Don't get many girls around these parts.\n\n";
					}
		
		cout << "What race are you?\n\nHuman, Orc, or Elf.\n\n";
		
		cout << "Humans get a balanced buff of +1 to all.\n\n";
		
		cout << "Orcs get +2 Attack and HP.\n\n";
		
		cout << "Elves get +2 Magic and HP.\n\n";
		
		cin >> race;
		
		if ( race == "Human" )
			{
				cout << "\nAh a human, the village through the forest will welcome you openly.\n\n";}
		
		if ( race == "Orc" )	{
					cout << "\nThe nearby village fears Orcs, show them kindness to\nbefriend them.\n\n";}
		
		if ( race == "Elf" )	{
			cout << "\nAn Elf you say? Mysterious folk you are.\n\n"; }
			
		cout << "Well that's enough about you, down to learning the basics.\n\n";
		
		cout << "General commands will bring your hero where you say\ndepending on the situation.\n\n";
		
		cout << "This game is case-sensative so type commands as you see on\nscreen.\n\n";
		
		cout << "You can attack, magic, or defend in combat.\n\n";
		
		cout << "All heros start with 10 Hit Points (HP), 5 Attack (A),\nand 2 Defense (D).\n\n";
		
		cout << "Now that you have learned some basics, lets jump into this.\n\n\n\n";
		
		cout << "You awaken in a forest circle, you don't know how you got\nhere and you feel like you're being watched.\n\n";
		
		cout << "You can see a path on your left with a bright light shining through, and a dark damp path to the right.\n\n";
		
		cout << "Which do you choose?\n\n";
		
		cout << "Left or Right\n\n";
		
		cin >> Direction;
		
		if ( Direction == "Left" ) {
			cout << "\nYou walk through the brightly lit path to an opening, the\nsun is shining so bright through the opening you can barely see. You step off a cliff and fall to your death.\n\n";
			cout << "GAME OVER";
			return 0;  }
		
			else {
				cout << "\nYou walk down the dark, dim path. You feel as if whatevers\nwatching you is getting closer. In the distance you hear\nvillage chatter. Suddenly a Giant Rat appears from the\nbushes!\n\n"; }
		
		cout << "Prepare for battle!\n\n";
		
		cout << "Giant Rat 10 HP, 3 Attack, 2 Defense.\n\n";
			
			
		return 0; 
	}


I'm teaching myself and only been at it for like 3 days learning from no prior knowlage.
I'm also doing this on my driod...
I would strongly recommend you learn about functions and put some of your code into well-named functions that main calls.

If you keep doing what you are doing, it's going to be very difficult to debug your program as it gets bigger and bigger all in the main function.

Example:
1
2
3
4
5
6
7
8
9
10
11
12
void printIntro();

int main()
{
    // Put some of those cout statements from main into the printIntro function.
    printIntro();
}

void printIntro()
{
    cout << "stuff, etc. etc." << endl;
}


If you do that, and you need to exit the program from a function that is not main, you can use the exit function. Include stdlib.h in your program, then you call exit(0); rather than return 0;

Good luck!
Thanks it is getting hard to keep starting over over.

Ill try that out
Topic archived. No new replies allowed.