y/n while loop

okay i had homework to make a choose your own adventure story but whenever i enter y to tryAgain it just says "press any key to continue. . ." here's my code:
1
2
3
4
5
6
7
8
9
10
11
 

#include <cstdlib>
#include <iostream>
#include <windows.h>

using namespace std;

int main(int argc, char *argv[])
{//*
    int 
etc...
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
    int yes_no;
    bool loop = true;
    char tryAgain;
    
    while (loop == true){
    
    cout << "Welcome to choose your own adventure!" << endl << endl;
    
    cout << "What would you like to be?  Type 1 for Goblin, 2 for elf, or 3 for fairy" << endl;
    cin >> firstInput;
    if(firstInput == 1){//**
                cout << "You are a Goblin." << endl << endl << "You are on a qu";
                cout << "est to retrieve your village's sacred emerald from the";
                cout << " vicious dark queen.";
                cout << " Your mission, should you choose to accept, is this: ";
                cout << "you will have to bypass her guards, break down her doo";
                cout << "r, and find the emerald" << endl;
                cout << endl << "Will you accept this mission?  [y-1/n-2]" << endl;
                cin >> yes_no;
                if(yes_no == 2){//
                          cout << "Wow you wimp." << endl;
                }//
                else if(yes_no == 1){//***
                          cout << "Good Choice." << endl << endl;
                          cout << "Firstly, you need to bypass her guards. ";
                          cout << "You can either dig with your goblin nose, ";
                          cout << "and try to go underneath her guards (enter 1)";
                          cout << " or try to fight your way through them (enter";
                          cout << " 2)" << endl;
                          cin >> gob_yes_choice;
                          if(gob_yes_choice == 1){//****
                                            cout << "You died. There were mines." << endl;
                          }//****
                          else if(gob_yes_choice == 2){//+
                               cout << "Good choice"<< endl;
                               cout << "Now you are inside the queen's castle.";
                               cout << endl << endl;
                               cout << "You must now break down a giant door, there are two tools at your disposal:";
                               cout << " a big cast iron lever (1), or a large rock (2)";
                               cout << endl << "The gaurds are coming and you only have time to try one.  Choose wisely." << endl;
                               
                               cin >> gob_yes_fight_choice;
                               
                               if(gob_yes_fight_choice == 1){//++
                                                   cout << "You died. The lever was made of iron, which goblins are allergic to." << endl;
                               }//++
                               else if(gob_yes_fight_choice == 2){//+++
                                    cout << "Good choice.";
                                    cout << "You have three major suspicions of where the emerald is: in the coffin (1), under the queen's bed (2),";
                                    cout << " or in a gigantic glass case with the emerald inside of it (3)." << endl;
                                    cin >> fin_gob_choice;
                                    
                                    if(fin_gob_choice == 2){
                                                      cout << "You died. Your choice was too loud and woke up the queen." << endl;
                                    }
                                    else if(fin_gob_choice == 1){
                                         cout << "CONGRATULATIONS!!! YOU FOUND IT!!!" << endl;
                                         Sleep(3000);
                                         cout << "but then the queen, whom you woke up by shouting ''I found it!'' stabbed you." << endl;
                                    }
                                    else if(fin_gob_choice == 3){
                                         cout << "Well yeah duh,";
                                         cout << endl << "Congrats you won...";
                                    }
                               }//+++
                          }//+
                }//***
    }//**
    else if(firstInput == 2){//-
         cout << "You are an Elf." 
etc...
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
    }//-
    else if(firstInput == 3){
         cout << "......";
         Sleep(1500);
         cout << "A fairy? ";
         Sleep(2000);
         cout << "Really? ";
         Sleep(2500);
         cout << "REALLY? ";
         Sleep(1500);
         cout << "What is this... ";
         Sleep(1000);
         cout << "Ameature hour?" << endl;
         Sleep(2000);
         cout << "That's just sad." << endl;
         Sleep(2000);
    }
    
    cout << "Would you like to try again? [y/n]" << endl;
    cin >> tryAgain;
    if(tryAgain == 'y'){
                loop = true;
    }
    else if(tryAgain == 'n'){
         loop = false;
    }
    
    system("PAUSE");
    return EXIT_SUCCESS;
    

    }//*
}



i need to know why it doesn't loop when i say yes to tryAgain...

please help. thanks.
Last edited on
Look at just the relevant code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

int main(int argc, char *argv[])
{
    bool loop = true;
    while (loop == true){ //open while bracket
    //Stuff
    cin >> tryAgain;
    if(tryAgain == 'y'){
                loop = true;
    }
    else if(tryAgain == 'n'){
         loop = false;
    }
    
    system("PAUSE");
    return EXIT_SUCCESS;
    

    }//closed while bracket
}


In what scenario would line 15 not execute?
would it execute even when it is false? i think i understand... okay so i should just move the End While } up to before the
1
2
 system("PAUSE");
return EXIT_SUCCESS; 
?
ah it works, thank you
Topic archived. No new replies allowed.