Random Number Problem

Hi, I'm moderately, (very), new to c++. I've been working on a little text game and I've come across a problem. When I have the player hit a lock at the beginning of the game, they continually hit it for the same amount of damage. I want it to be a random number from 1-3. The, little random code I have already is what I found on Google. Here's the code. What you're looking at to fix is the BASICS, LEVEL 1 PLAYERs int dmg code thing, and if that's not where the problem is, go down to the Jail Cell comment's do while loop. Thanks.

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*/;
   
   
   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");
   
   //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 );  
             
   

       
   
   
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
Last edited on
If you calculated dmg after line 66, would it give you the results you are looking for?
Calculate.. as in??.. Assign it? Like, dmg = 3? Because I want it to be random. If you do in fact mean 'calculate', I'm sorry, I'm a newb, please explain.. ^^
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
    if( choice == 1 ) {
        cout << "You hit the lock. You do " << dmg << " damage to the lock.\n";
        enemycurrenthp = enemycurrenthp - dmg;
        dmg = (rand() % 3);  
        }
    else {
        cout<<"That wasn't a choice!\n";
    }
} while (enemycurrenthp > 0 );  
             
   

       


What I did is assign a new random number to dmg.
Though I'm not so sure...
Let me know if it worked.

Last edited on
It worked, thanks a lot!
Last edited on
Trying deleting "int dmg = (rand() % 3) + 1", in line 15 and adding it right before line 67, in the if statement.

Like this:
1
2
3
4
5
6
    if( choice == 1 ) {
        int = dmg = (rand() % 3);  
        cout << "You hit the lock. You do " << dmg << " damage to the lock.\n";
        enemycurrenthp = enemycurrenthp - dmg;
    } else
        cout<<"That wasn't a choice!\n";

This way, it's not repetitive and makes use of local scopes.
Last edited on
Topic archived. No new replies allowed.