Text-Based RPG Game problem

I am working on a text-based RPG game and every time I run the code, it just repeats the first cout's over and over again. The one thing that it
does that I wanted it to do is when I push the 'n' || 'N' button it does end the game. For some reason I cannot understand why it keeps repeating
like it is.

If anyone can help me and show me what I am doing wrong it would be greatly appreciated. Thank you in advance.
here is my source code:

P.S. I am using NetBeans as my IDE, not sure if that is the issue or not, don't think that it should matter but just thought I would say it anyway.

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
//Author/Programmer: Jessee Lahmann
//Date Started: December 18, 2012 @ 1:00PM
//Last Date Worked On: December 23 @8:00PM
//Current Version: Prelude-Peril of Realms 0.1
//Current Readme Version: 0.1
//Type of Program: Text-Based RPG
//copywrite 2012

#include <iostream>
#include <string>
using namespace std;

int main()
{
     //Title of the Game - Text-Based RPG ##version 0.1##
     cout <<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
     cout <<"~Prelude - The Peril of Realms v.0.1~" << endl;
     cout <<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
     cout << "Written & Programed By: Jessee Lahmann" << endl;
     cout << " " << endl;
     cout << " " << endl; 
     cout << "Push the 'y' key and press <Enter> to play" << endl;
     cout << "Push the 'n' key and press <Enter> to exit" << endl;
     
     while(true)
     {
          char userInput;
          cin >> userInput; 
          
          cout << "This is the story of how the current king came into power." << endl;
          cout << "If at anytime you wish to exit the game, just push the 'q key" << endl;
          cout << "and press <Enter>. Note: Your game will not be saved." << endl;
          
          if(userInput == 'y' || userInput == 'Y')
          {
              
               cout << "continue... Yes(y) or No(n)?" << endl;
               continue;
               
          }
          else if(userInput == 'n' || userInput == 'N')
          {
               cout << "Thank you for playing!" << endl;
               break;
          }

          cin >> userInput;
          if(userInput == 'y' || userInput == 'Y')
          {
               cout << "In this story you will guide the hero to kingship." << endl;
               cout << "continue... Yes(y) or No(n)?" << endl;
               break;
          }
          else(userInput == 'n' || 'N');
          {
               cout << "Thank you for playing!" << endl;
               break;
          }

          cin >> userInput;

          if(userInput == 'y' || userInput == 'Y')
          {

               continue;
          }
          else if(userInput == 'n' || userInput == 'N')
          {
               cout << "Thank you for playing" << endl;
               break;
          }
     }
}
Your statement at line 54 has a few problems:
else(userInput == 'n' || 'N');

1) Missing if
2) Comparison is incorrect. 'n' || 'N' will always evaluate to true.
3) You have a semicolon after the condition which terminates the statement.

It should be:
else if (userInput == 'n' || userInput == 'N')
Thank you very much for pointing out those mistakes, but it is still repeating this line of code:

1
2
3
     cout << "This is the story of how the current king came into power." <<endl;
    cout << "If at anytime you wish to exit the game, just push the 'q key" <<endl;
    cout << "and press <Enter>. Note: Your game will not be saved." << endl;

not sure why it is doing this.

I mean that it is not moving on to this line of code:

1
2
3
4
5
6
7
          cin >> userInput;
          if(userInput == 'y' || userInput == 'Y')
          {
               cout << "In this story you will guide the hero to kingship." << endl;
               cout << "continue... Yes(y) or No(n)?" << endl;
               break;
          }
Last edited on
The problem is you are using continue statements incorrectly. When you use continue it skips everything else that is after it inside the loop and goes to the next iteration of that loop if possible (which in this case is just the beginning of your infinite while loop). So it never sees any of the other if statements.

Remove the continues and it should progress normally.
Topic archived. No new replies allowed.