Problem with my while loop

Hey guys and Gals,

My first time asking for help like this but I see you guys are very helpful so I will give it a shot. So I have a simple program for a class and I can get the program to run once and I have the structure down I think. The Problem I am having is when it comes to the end and the user chooses the correct box, I want them to decide if they want to continue or not and if they do then they get to "level up" and play again. simple stupid game but I am having trouble with the last part and I can't figure out where I am going wrong with it.

In my while statement I was thinking I needed the two conditions to continue the statement but i cant figure out how to code it properly.

I was thinking it would be something like this

while ( choice != 5 && play =='y)

I know it's not right but I can't think of how to make that part work.

next part is tracking the level up. and actually making it work. I feel like I am close and if the while loop worked properly the level part might work.

Thanks in advance for any help on 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
  #include <iostream>
#include <string>

using namespace std;


struct Player
{
        string name;
};

void WelcomePlayer(Player& hunter)
{
        cout << "*****Welcome to the Treasure Hunt ya Pirate!*****" << endl << endl;
        cout << "What is your name?" << endl << endl;

        cin >> hunter.name;

        cout << endl << "Get to Huntin!! " << hunter.name << endl;
 }

int main()
{

   int choice = 0;
   //int level = 1;
   //char play = 'A';

    string chests[] = { "Wrong", "Wrong", "Wrong", "Wrong", "Correct"};

    Player hunter;
            WelcomePlayer(hunter);


    while (choice != 5){

    cout << "Pick a number between 1 and 5 to choose a \n Chest to open to see if you find the treasure \n";
    cin >> choice;


    if (choice == 1){
        cout << chests[0] << endl;
        cout << "chest.  Try again!" << endl;
    }

    else if (choice == 2){
        cout << chests[1] << endl;
        cout << "chest.  Try again!" << endl;

    }
    else if (choice == 3){
        cout << chests[2] << endl;
        cout << "chest.  Try again!" << endl;

    }
    else if (choice == 3){
        cout << chests[2] << endl;
        cout << "chest.  Try again!" << endl;

    }
    else if (choice == 4){
        cout << chests[3] << endl;
        cout << "chest.  Try again!" << endl;

    }
    else{
        cout << chests[4] << endl;


    cout << "Congratulations!\n  You found the treasure!\n" << hunter.name << " Leveled Up!\n";
    //cout << "Do you want to keep playing?  Press 'y' to continue or 'n' to quit \n";
   // cin >> play;


   // if (play != 'y'){

       // cout << "You quitter!";
   // }
  //  else{

       // level++;
    }

}


    return 0;
}
You can use the break; to leave the node:
1
2
3
4
5
6
7
8
9
    cout << "Do you want to keep playing?  Press 'y' to continue or 'n' to quit \n";
   cin >> play;


   if (play != 'y'){

       cout << "You quitter!";
       break; // Note: This ends the loop
   }
So I tried that and all it does now is ask for the name then ends the game without ever going through the loop.

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

using namespace std;


struct Player
{
        string name;
};

void WelcomePlayer(Player& hunter)
{
        cout << "*****Welcome to the Treasure Hunt ya Pirate!*****" << endl << endl;
        cout << "What is your name?" << endl << endl;

        cin >> hunter.name;

        cout << endl << "Get to Huntin!! " << hunter.name << endl;
 }

int main()
{

   int choice = 0;
   int level = 1;
   char play = 'A';

    string chests[] = { "Wrong", "Wrong", "Wrong", "Wrong", "Correct"};

    Player hunter;
            WelcomePlayer(hunter);


    while (choice != 5 && play == 'y'){

    cout << "Pick a number between 1 and 5 to choose a \n Chest to open to see if you find the treasure \n";
    cin >> choice;


    if (choice == 1){
        cout << chests[0] << endl;
        cout << "chest.  Try again!" << endl;
    }

    else if (choice == 2){
        cout << chests[1] << endl;
        cout << "chest.  Try again!" << endl;

    }
    else if (choice == 3){
        cout << chests[2] << endl;
        cout << "chest.  Try again!" << endl;

    }
    else if (choice == 3){
        cout << chests[2] << endl;
        cout << "chest.  Try again!" << endl;

    }
    else if (choice == 4){
        cout << chests[3] << endl;
        cout << "chest.  Try again!" << endl;

    }
    else{
        cout << chests[4] << endl;


    cout << "Congratulations!\n  You found the treasure!\n" << hunter.name << " Leveled Up!\n";
    cout << "Do you want to keep playing?  Press 'y' to continue or 'n' to quit \n";
    cin >> play;
    level++;

    if (play != 'y'){

        cout << "You quitter!";
        break;
    }

   
    }

}


    return 0;
}
On line 27 you do:
 
char play = 'A';

Then on line 35, you put the following statement
play=='y' which returns false. Initialize play as 'y'.
Topic archived. No new replies allowed.