How to stop the game from ending after I press a button?

I recently made a text based game with an inventory but whenever I open the inventory the game stops. How do I stop this?

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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <iostream>
using namespace std;

string endtextfunct = "Oops you died";
int question;
int attachedpaper = 0;
int key = 0;
int itemsavewhenrestart = 0;

int main (){
    cout << "Yusuf Presents: ";
    cin.ignore();
    cout << endl;
    cout << "Part 1 of Adventures in Vetteres";
    cin.ignore();
    cout << endl;
    cout << "If you have item numbers enter it, if not enter 0: ";
    cin >> itemsavewhenrestart;
    cout << endl;
    cout << "You walk through a laboratory. You see glasses filled with unknown substances. 1= Inspect the glasses, 2= Read the papers lying on the tables, 3= Open the door on the door on the far side of the room: ";
    cin >> question;
    cout << endl;
    
if (itemsavewhenrestart == 12){
    ++attachedpaper;
    ++key;
}

if (itemsavewhenrestart == 21){
    ++attachedpaper;
    ++key;
}

if (itemsavewhenrestart == 1){
    ++attachedpaper;
}

if (itemsavewhenrestart == 2){
    ++key;
}

if (question == 1){ //QUESTION 1
    cout << "You inspect the tubes. You see several chemicals. 1= Open the glass tubes, 2= Put the glass tubes back: ";
    cin >> question;
    cout << endl;
    
    if (question == 1){ // subQUESTION 1 // DEADEND
        cout << "You opened the glass tubes";
        cout << endl;
        cout << endtextfunct;
        cout << endl;
        cout << "The substances contained inside were poison";
        cout << endl;
        cout << "THE END";
        return 0;
    }
    
    if (question == 2){ // subQUESTION 2 // DEADEND
        cout << "You replace the bottles back to their former spot. There seems to be nothing else to do so you return home. (play again)";
        cout << endl;
        return 0;
    }
}

if (question == 2){ // QUESTION 2
    cout << "You read the papers on the desk. Hmm...some classified information. Would you like to continue reading? 1= Yes, 2= No: ";
    cin >> question;
    cout << endl;
    
    if (question == 1){ // subQUESTION 1
        cout << "There seems to be parts blanked out on the paper. PAPER: The F.r..la is almost ready. I still can't beleive I made a f.r..la for eternal y..t. - Dr C. Tallow. 1= Try to guess the blanked out words, 2= Read the paper attached to it: ";
        cin >> question;
        cout << endl;
        
        if (question == 1){ // subsubQUESTION 1
            cout << "You try to guess the blanked words, this is what you come up with. F.r..la = Formula, y..t. = youth. 1= Read the attached paper, 2= Go home: ";
            cin >> question;
            cout << endl;
            
            if (question == 1){
                cout << "You read the attached paper. It seems like it is just a random string of letters. 1= Keep the paper, 2= Throw the paper: ";
                cin >> question;
                cout << endl;
                
                if (question == 1){
                    cout << "You put the attached paper in your bag.";
                    attachedpaper = 1;
                    cout << endl;
                    cout << "Congratulations! You sucessfully completed part one of Adventures in Vetteres! The attached papers item number is 1, enter it in the start of the game to have the attached paper in your inventory. ";
                
                if (question ==2){
                    cout << "Bad Decision!";
                    return 0;
                }
            }
        }
    }
        
        if (question == 2){ // subsubQUESTION 2
            cout << "You read the attached paper. It seems like it is just a random string of letters. 1= Keep the paper, 2= Throw the paper: ";
            cin >> question;
            cout << endl;
            
            if (question == 1){
                cout << "You put the attached paper in your bag.";
                attachedpaper = 1;
                cout << endl;
                cout << "Congratulations! You sucessfully completed part one of Adventures in Vetteres! The attached papers item number is 1, enter it in the start of the game to have the attached paper in your inventory. ";
                return 0;
                
                if (question ==2){
                    cout << "Bad Decision!";
                    return 0;
                }
            }
        }
    }
}
    if (question == 2){ // subQUESTION 2 //DEADEND
        cout << "There seems to be nothing to do. (play again) ";
        cout << endl;
        return 0;
    }

if (question == 3){ // QUESTION 3
    cout << "You try to open the door but it needs a one-digit password. There seems to be some words on it: 600000 / 100000. Try to find the correct answer. 1= 1, 2= 6: ";
    cin >> question;
    cout << endl;
    
    if (question == 1){ //subQUESTION 1 // DEADEND
        cout << "Wrong password! (play again) ";
        return 0;
    }
    
    if (question == 2){ // subQUESTION 2
        cout << "Correct! The door clicks open. And within is a treasure chest. 1= Open the chest, 2= Get out of the laboratory immediately: ";
        cin >> question;
        cout << endl;
        
        if (question == 1) { // subsubQUESTION 1
            cout << "You open the treasure chest and inside is a key.";
            key = 1;
            cin.ignore();
            cout << endl;
            cout << endl;
            cout << "Congratulations you completed part one of Adventures in Vetteres! The keys item number is 2, enter it in the start of the game to have the key in your inventory. ";
        return 0;
        }
        
        if (question == 2){ // subsubQUESTION 2 // DEADEND
            cout << "There seems to be nothing to do. (play again) ";
            cout << endl;
            return 0;
        }
    }
}

if (question == 4){
    cout << "Inventory: " << "Attached paper: " << attachedpaper << ", " << "Key: " << key;
}

    return 0;
}
Last edited on
Hello Tallow,

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
#include <iostream>
#include <string>

#include <chrono>
#include <thread>

using namespace std;  // <--- Best not to use.

string endtextfunct{ "Oops you died" };  //  <--- Make a "const" unless this needs to change.
int question{};
int attachedpaper{};
int key{};
int itemsavewhenrestart{};

int main()
{
    cout << "Yusuf Presents:\n\n";
    //cin.ignore();
    //cout << '\n';
    std::this_thread::sleep_for(std::chrono::seconds(2));  // Requires header files "chrono" and "thread".
    
    cout << "Part 1 of Adventures in Vetteres\n\n";
    //cin.ignore();
    //cout << '\n';
    std::this_thread::sleep_for(std::chrono::seconds(2));

    cout << "If you have item numbers enter it, if not enter 0: ";
    cin >> itemsavewhenrestart;
    //cout << '\n';
    cout <<
        "\n"
        "You walk through a laboratory. You see glasses filled with unknown substances.\n"
        "1 = Inspect the glasses,\n"
        "2 = Read the papers lying on the tables,\n"
        "3 = Open the door on the door on the far side of the room.\n"
        "  Enter your Choice: ";
    cin >> question;

Over all prefer to use the new line (\n) over the "endl"s. "endl" is a function that takes time. The more you have the more you wait.

In lines of code like this:
1
2
std::cout << ....
std::cin >> ....

The "std::cin: will flush the output buffer B4 any input is allowed. Something that works to your advantage. Unfortunately the oposit is not true.

I noticed that many of your if statements end with "return 0;". This will exit the program. I hope that you are not expecting something else to happen. If so you will need to explain what you need to do.

The 2 extra, last 2, header files are a suggestion. Not sure what you intended in the first place, but using the "ignore" leaves the user wondering what to do.

The "<string>" header file is required.

Revising line 30 makes it easier to work with. You may want to consider adding a choice for "Exit" of the program.

The revised code has this output:

Yusuf Presents:

Part 1 of Adventures in Vetteres

If you have item numbers enter it, if not enter 0: 0

You walk through a laboratory. You see glasses filled with unknown substances.
1 = Inspect the glasses,
2 = Read the papers lying on the tables,
3 = Open the door on the door on the far side of the room.
  Enter your Choice:


There is a 2 second pause B4 lines 2 and 3 display.

The uniform initializer, the {}s, are much easier to use than the " = 0".
https://www.informit.com/articles/article.aspx?p=1852519
And or
https://stackoverflow.com/questions/18222926/why-is-list-initialization-using-curly-braces-better-than-the-alternatives

Andy

Edit: spelling.
Last edited on
Thank you Andy, the cin.ignore() was my alternative for the sleep fuction because I didnt understand it. The return 0 s at the end of the if function were supposed end the program when the user inputed awrong answer. I wanted the game to show the inventory when the user inputed 4 but after it shows it it stops the program. Also what does return 0 do?
Hello Tallow,

The problem with using the "cin.ignore()" is that without a prompt no one knows it is waiting for you to press enter.

It may appear a bit strange or different, but there is only 2 parts to worry about. "seconds" means the number in () is a whole number of seconds. If you use "milliseconds" the the number in () starts @ 1000 for 1 second and any number above or below that is part of a second. I have found that any number close to or < 250 is not enough time, but 1250 milliseconds may make a little difference.

These days I do not write the line. I have it set up in my IDE to just paste it in then put a number in the ().

The (0) in return 0; means the the program ended and there was no problem. "EXIT_SUCCESS" and "EXIT_FAILURE" are defined in the "<cstdlib>" header file. Although "cppreference", https://en.cppreference.com/w/c/program/EXIT_status , says that the value of constants is "implementation defined" I have found that "SUCESS" is usually (0) zero and "FAILURE" is (1), but do not count on this. You will need to check the way your IDE.compiler header files define these variables. Usually I just (0)zero at the end of "main" and 1 or greater anywhere else. Actually any non (0)zero value will work to mean that there is a problem or that you made an exit from somewhere other than the end of "main". Personally I like using positive numbers, but negative numbers work. Its your choice.

I have found that when a return is not at the end of "main" it helps to make the returns 1, 2, 3 etc. This helps to know or track down where the program ended.

Last night after running the program several times and reading your last post I have some suggestions that you might want address.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Yusuf Presents:

Part 1 of Adventures in Vetteres

If you have item numbers enter it, if not enter 0: 0

You walk through a laboratory. You see glasses filled with unknown substances.
1 = Inspect the glasses,
2 = Read the papers lying on the tables,
3 = Open the door on the door on the far side of the room.
  Enter your Choice: 1

You inspect the tubes. You see several chemicals.
1= Open the glass tubes,
2= Put the glass tubes back: 1

You opened the glass tubes.
Oops you died!
The substances contained inside were poison.

     THE END

On line 5 you could enter 1, 2, 12 or 21. The 1 or 2 may be easy to figure out, but the 12 or 21 is a problem with some instruction to use 1 of these numbers at the first prompt.

The menu is lacking a choice 4 to check the inventory. and maybe a 5 to exit.

Line 18 is fine, but when you define and initialize the variable you should make it a constant: const string END_TEXT_FUNCT{ "Oops you died!" }; since you want to make sure that you do not accidentally change it.

Line 21. Adding the blank line and indenting is just something I like to do. It is not required, IMHO I think it looks better. I use the same idea with error messages to catch the users attention.

As for the "question == 4" first I would put the sleep line I showed you after the "cout" to pause long enough to read the message. Also I would put most of "main" in a do/while loop because after this prints you need to start over not end the program just to check your inventory. Also the do while loop would allow you to continue the program starting over instead of exiting and rerunning the program. Something to consider.

In this output:

Yusuf Presents:

Part 1 of Adventures in Vetteres

If you have item numbers enter it, if not enter 0: 0

You walk through a laboratory. You see glasses filled with unknown substances.
 1 = Inspect the glasses,
 2 = Read the papers lying on the tables,
 3 = Open the door on the door on the far side of the room.
  Enter your Choice: 2

You read the papers on the desk. Hmm...some classified information.
Would you like to continue reading?
 1 = Yes,
 2 = No: 1

There seems to be parts blanked out on the paper.
PAPER: The F.r..la is almost ready.
I still can't beleive I made a f.r..la for eternal y..t. - Dr C. Tallow.
 1 = Try to guess the blanked out words,
 2 = Read the paper attached to it: 1

You try to guess the blanked words, this is what you come up with.
F.r..la = Formula, y..t. = youth.
 1 = Read the attached paper,
 2 = Go home: 1

You read the attached paper. It seems like it is just a random string of letters.
 1 = Keep the paper,
 2 = Throw the paper: 1

You put the attached paper in your bag.
Congratulations! You sucessfully completed part one of Adventures in Vetteres!
The attached papers item number is 1,
enter it in the start of the game to have the attached paper in your inventory.


With your original code some of the lines displayed did not fit in mu console window and may not fit a lap top screen without wrapping when it reaches the right. When you write your code you need to think about what is displayed on the screen. If nothing else write a small test program that you can use to test your "cout" statements and see how they look on the screen B4 you put them in your program.

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
// <--- Most comon includes.
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>

#include <chrono>
#include <thread>

int main()
{
    std::cout << "\n\n Your Original \"cout\"\n\n";

    std::cout << "Correct! The door clicks open. And within is a treasure chest. 1= Open the chest, 2= Get out of the laboratory immediately: ";

    std::cout << "\n\n Corrected \"cout\"\n\n";

    std::cout <<
        "Correct! The door clicks open. And within is a treasure chest.\n"
        " 1 = Open the chest,\n"
        " 2 = Get out of the laboratory immediately: ";

    std::cout << "\n\n Revised \"cout\"\n\n";

    std::cout <<
        "Correct! The door clicks open. And within is a treasure chest.\n"
        " 1 = Open the chest,\n"
        " 2 = Get out of the laboratory immediately!\n"
        "   Enter choice: ";



	// <--- Keeps console window open when running in debug mode on Visual Studio. Or a good way to pause the program.
	// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
	//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue: ";
	std::cin.get();

	return 0;  // <--- Not required, but makes a good break point.
}

You would not save this with all the "cout" statements, but this does give the output of:

 Your Original "cout"

Correct! The door clicks open. And within is a treasure chest. 1= Open the chest, 2= Get out of the laboratory immediate
ly:

 Corrected "cout"

Correct! The door clicks open. And within is a treasure chest.
 1 = Open the chest,
 2 = Get out of the laboratory immediately:

 Revised "cout"

Correct! The door clicks open. And within is a treasure chest.
 1 = Open the chest,
 2 = Get out of the laboratory immediately!
   Enter choice:

 Press Enter to continue:


In the 1st "cout" that is the way it looks in my console window. Not the easiest line to read. The 2nd output shortens the lines and makes a menu that looks better. In the 3rd output the layout of the "cout" in the program made it very easy to add the last line and test how it looks. Notice that the (" Enter choice: ";) has no "\n" or "endl". This puts a following "cin" on the same line. The spaces at the beginning of the string are not required, but the indentation looks nicer.

Andy
Again, thank you Andy for your informative answers to all my questions. I will try to learn more about all the wonderful details you have given and incorporate it in my game.

Tallow
Hello Tallow,

At the beginning of your program you have these 2 if statements:
1
2
3
4
5
6
7
8
9
10
11
if (itemsavewhenrestart == 12)
{
    ++attachedpaper;
    ++key;
}

if (itemsavewhenrestart == 21)
{
    ++attachedpaper;
    ++key;
}

Each 1 does the same thing.

You can combine them into just 1:
1
2
3
4
5
if (itemSaveWhenRestart == 12 || itemSaveWhenRestart == 21)
{
    ++attachePpaper;
    ++key;
}


Thank you. That was fun to work on.

Andy
Topic archived. No new replies allowed.