Maybe a "noob" question

Well im just starting out on the C++ scene and for an assignment i have to program a small C++ game. So i went for the generic Text Based RPG and planned on implementing a Random Number Generator for some combat stuff.

The problem i am finding is this;

I have a generic number generator

1
2
3
4
srand ( time(0) );
	for (int t=0;t<10;t++)
	int Damage;
    Damage = rand() % 9 + 1;


Now the problem i am finding is every time i call the Damage variable to see if it is a critical hit or a normal hit the result is always the same, how can i get it to clear the variable and give me a new number every time i call the variable?

Cheers,
Josh

(Hopefully not a totally stupid question/problem!)
Is that the exact loop syntax? 'Cause that loop won't be doing anything apart from declaring Damage. Have you got a full code snippet?

#include <iostream>
#include <string>
#include <ctime>
#include <windows.h>
#include <cstdlib>
#include <conio.h>
#include "Classes.h"

using namespace std;

int main()
{
srand ( time(0) );
for (int t=0;t<10;t++)
int Damage;
Damage = rand() % 9 + 1;

{
if (Damage >= 7)
cout << "You hit the guard for a critical hit! \n";

}

{
if (Damage < 7)
cout << "You hit the guard for a normal swing \n";

}
{
cin.get();
}


Is that what you are looking for? Thats all the code i have at the moment, i just wanted to get the number generator to keep generating different numbers instead of only generating one number.

use { when you enter the loop and } when you finish it

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

int main()
{
srand ( time(0) );
for (int t=0;t<10;t++){
int Damage;
Damage = rand() % 9 + 1;

{
if (Damage >= 7)
cout << "You hit the guard for a critical hit! \n";

}

{
if (Damage < 7)
cout << "You hit the guard for a normal swing \n";
}
Yeah, jmrag is right. If you're looking to get 10 outputs here (i.e. - 10 attacks), then you need to encompass the if statements and the Damage assignment in the for loop using braces.

Not as necessary to have the Damage declaration within the loop, though, although it shouldn't really do any real harm right now.
Thank you for that Jmrag. Now what is the best way to almost force the generator to give me a new number when engaging in combat. So for example You hit that guard for a critical and kill him instantly. If another guard comes along how do i make it give me a new number?

Like i said im really new to this :(
It *should* be picking a new number through each iteration of the loop as it is. Every time you call that rand function, it should pick a number between one and nine using the rand algorithm and a seeded number from the system clock.
Damage = rand() % 9 + 1;

If that is in the for loop as well, it'll pick a new number [1,9] at every round.
Topic archived. No new replies allowed.