Do-while loop ends

My do-while loop ends my program. I know I'm probably doing something wrong, I'm a newb.




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
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;


int main(int argc, char *argv[])
{
   /*enemies*/ int enemydmg; int enemymaxhp; int enemycurrenthp;
   /* Lock hp = 5, 0 dmg*/;
   // Rooms//
   int room1done; int room2done; int room3done; 
   srand((unsigned)time(0));
   //BASICS, LEVEL 1 PLAYER //
   int maxhp = 100 ; int currenthp = 100; int dmg = (rand() % 3) + 1;; int ap = 0;
   //RANDOM STUFF//
   string name;
   int choice;
   
   














  // Introduction //


   cout << "\t\t--WELCOME TO TEXTVENTURE--\n";
   cout << "Hello. What is your name?\n";
   cin >> name;
   cout << "Your name is now " << name << "\n";
   cout << "Hello, and welcome, " << name << ", you are about to enter the world";
   cout << " of Textventure.";
   cout << "\nYou make selections on what you do by typing any number option given.\n";
   cout << "Typing something other than the options given will bring up an error.\n";
   cout << "You have health. You may be damaged by monsters, or the result of \n";
   cout << "another choice you made. You will encounter monsters and must fight.\n";
   cout << "When you fight, or complete other special encounters, you will earn ap,\n";
   cout << " aka adventure points, which can make you level up and up your stats.\n";
   cout << "There are other ways to raise your stats, anyways, good luck!\n";
   cout << "Did you get that? ^^ ";
   system("PAUSE");
   
   //Room 1:  JAIL CELL//
   
   cout << "\n\nYou awake in a jail cell.\n";
   cout << "It's dark and you can only remember your name.\n";
   cout << "In this room is only one cell, and a door out. Your cell is locked however.\n";
   cout << "What would you like to do?\n";
   cout << "1. Try and break the lock with your fists.\n2. Try and squeeze through the bars.\n";
    enemycurrenthp = 5;
    enemymaxhp = 5;       
          do {
    cin >> choice;
    
    if( choice == 1 ) {
        cout << "You hit the lock. You do " << dmg << " damage to the lock.\n";
        enemycurrenthp = enemycurrenthp - dmg;
        }
    else {
        cout<<"That wasn't a choice!\n";
    }
  } while (enemycurrenthp > 0 );           
   cout << "The lock breaks open.\n";
   cout << "What would you like to do?\n";
   cout << "1. Continue through the door.\n2.Search the room.\n";
   
       
    std::cout << "Press ENTER to continue...";
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
    
    return EXIT_SUCCESS;
}
Please explain what you expect v/s what are you getting.
I think your loop is fine it's your window closing.

I shouldn't be giving advice to anyone yet, but your code runs fine for me when I swap out lines 79/80 with something else
closed account (z05DSL3A)
At line 65 put in std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );.
Basically the stream is no clear when it gets to line 80 so that can clear the stream and carry on to end the program.

Edit:
Handling the input stream is very important. With the code as it is, if I enter 'Grey Wolf' as my name only Grey will be taken out of the stream. you then get to system("PAUSE"); I press any key and the program will loop with the message "That wasn't a choice!" because the stream still has Wolf in it and it fails to extract a number.

It would be worth you spending a little time learning how to handle user input.
Last edited on
Thanks. I'm really new to this stuff and have been programming solely off of internet tutorials. I plan to get an actual book soon.
closed account (z05DSL3A)
plantking6 wrote:
I plan to get an actual book soon.
I would recommend
C++ Primer
by Stanley B. Lippman, et al

http://www.amazon.co.uk/gp/product/0201721481/ref=s9_simh_gw_p14_d0_i1?pf_rd_m=A3P5ROKL5A1OLE&pf_rd_s=center-2&pf_rd_r=0K1Q608B40PPZP2A0XF5&pf_rd_t=101&pf_rd_p=467128533&pf_rd_i=468294

Have fun.
Topic archived. No new replies allowed.