how to keep the cmd prompt open

i want it to display my inventory and stay there if i select 'y' when it asks me to instead of immediately closing after a split second. how do i accomplish 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
  #include <iostream>
#include <string>
using namespace std;
int main ()
{
const int MAX_ITEMS = 5;
string backPack[MAX_ITEMS];
int numItems = 0;
backPack[numItems++] = "sword";
backPack[numItems++] = "shield";
backPack[numItems++] = "gloves";
backPack[numItems++] = "boots";

for (int i =0; i < numItems; ++i)
{
    
    cout << "Your item number " << i+1 <<" is: " << backPack[i] << endl;
}
cout << "you stumble upon a potion sitting there pointlessly. pick it up?: (y/n)\n";
char pickup;
cin >> pickup;
if (pickup == 'y')
{
          backPack[numItems++] = "potion";
          cout << "you pick up the potion\n";
           cout << "\nNow your items are: "; 
              for (int i =0; i < numItems; ++i)
              cout << backPack[i] << endl;
          }
          else
          {
              cout << "you ignore the potion.\n";
              cout << "\nNow your items are: "; 
              for (int i =0; i < numItems; ++i)
              cout << backPack[i] << endl;
if (numItems < MAX_ITEMS)
{
backPack[numItems++] = "potion";
cout << "you still have inventory room\n";
}
else
{ 
    cout << "cant carry anymore\n";
}
system("PAUSE");
}
}

You can use cin.get(); as a variation of system("PAUSE");

I would really recommend not using system("PAUSE");
It is slow and OS dependent.

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
#include <iostream>
#include <string>
using namespace std;
int main ()
{
const int MAX_ITEMS = 5;
string backPack[MAX_ITEMS];
int numItems = 0;
backPack[numItems++] = "sword";
backPack[numItems++] = "shield";
backPack[numItems++] = "gloves";
backPack[numItems++] = "boots";

for (int i =0; i < numItems; ++i)
{
    
    cout << "Your item number " << i+1 <<" is: " << backPack[i] << endl;
}
cout << "you stumble upon a potion sitting there pointlessly. pick it up?: (y/n)\n";
char pickup;
cin >> pickup;
if (pickup == 'y')
{
          backPack[numItems++] = "potion";
          cout << "you pick up the potion\n";
           cout << "\nNow your items are: "; 
              for (int i =0; i < numItems; ++i)
              cout << backPack[i] << endl;
              cin.get();
              cin.get();
              return 0;
              
          }
          else
          {
              cout << "you ignore the potion.\n";
              cout << "\nNow your items are: "; 
              for (int i =0; i < numItems; ++i)
              cout << backPack[i] << endl;
if (numItems < MAX_ITEMS)
{
backPack[numItems++] = "potion";
cout << "you still have inventory room\n";
}
else
{ 
    cout << "cant carry anymore\n";
   
}
cin.get();
cin.get();
return 0;
}
}
closed account (3qX21hU5)
I would recommend using

1
2
std::cout << "Press ENTER to continue...";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');


What this does is it will keep the window open until the user presses enter even if there is already unread '\n' characters in the buffer which can happen sometimes.

And as Blanchy said don't use system("PAUSE"); because it is a security risk, most AV programs will hate it and send you warning about it, it is not cross platform, and other reasons.
Topic archived. No new replies allowed.