Looping a Nested If w/ random number generator

Last edited on
I have no idea what you want us to answer here. The code that you provided currently does nothing at all. Did you try and loop it? Can you show us how you tried?
As it stands, it does nothing. It's 500+ lines of code. :P

This is just the first variable. I'll post a little more, but I'm trying to figure out why the random is spitting out the same number when I looped it.

Also, sorry, I put the wrong code above. This is the one where I actually tried to loop it.

#include <tchar.h>
#define _tWinMain wWinMain
#include <iostream>
#include <ctime>
#include <string>
using namespace std;

int _tmain()
{
int number;
srand(time(NULL));
double random;
float time;
random = (rand() % (100) + 1);
double gold;
gold = (random * 5);
double d10;
double d102;
double weapon;
weapon = (rand() % (10) + 1);
d10 = (rand() % (10) + 1);
d102 = (rand() % (10) + 1);
bool formal = "";
double quartz;
double quartz2;
double quartz3;
double insect;
double oak;
double fault;
double dragon;
double scales;
double cactus;
double blooms;
double moredust;
double moreshell;
double morescales;
double moreblooms;
double ruby;
double silver;
double hide;
double enchant;
double enchant2;
quartz = (rand() % ((10) + 1)*5);
quartz2 = (rand() % (10 + 1)*10);
quartz3 = (rand() % (10 + 1)*20);
insect = (rand() % (10) + 1);
oak = (rand() % (10 + 1))/2;
fault = (rand() % (10 + 1)/3);
dragon = (rand() % (10 + 1)/5);
scales = (rand() % (10 + 1));
cactus = (rand() % (10 + 1));
blooms = (rand() % (10 + 1) *2);
moredust = (rand() % (10 + 1)/2);
moreshell = (rand() % (10 + 1)/3);
moreblooms = (rand() % (10 + 1)*3);
ruby = (rand() % ((10) + 1))/5;
silver = (rand() % (10 + 1)/5);
hide = (rand() % ((10) + 1)/5);
enchant = (rand() % (10 + 1) / 3);
enchant2 = (rand() % (10 + 1) / 2);
morescales = (rand() % (10 + 1) * 2);

cout << "How many times are we rolling today?";
std::cin >> number;
for (int x = 0; x < number; x++)
{
I dont see a loop anywhere in your code. Also, Im not sure what this is int _tmain() Should be int main.
At the very bottom is where the loop starts. It loops a number of times equal to user input of "number".
Again, I can't put the full code on here, unless you want me to put it piecemeal, because it's very, very long.
put your code on www.pastebin.com and post the link by editing your first post.
Sorry I completely missunderstood your question. I know what you mean now. uhm...

Could you tell me in words what you want this piece of code to do - enchant = (rand() % (10 + 1) / 3);
There it is.
Every single one of your if statements wont work.

1
2
3
4
5
6
7
8
9
 
                        if (weapon = 1)
                        {
                                cout << "You get a Cold Wrought Iron Mace or Hammer!";
                        }
                        else if (weapon = 2)
                        {
                                cout << "You get a Cold Wrought Iron Mace or Hammer!";
                        }


You have to use ==, not =.

 
if(weapon == 1), else if(weapon == 2)
...Now I'm embarrassed. Can't believe I forgot something so fooking basic. :(

I'll fix it and get back to ya with the result in a bit.
Update: Program still loops into the same results every time. The variables spit out the same number. (restarting the full program spits out different results, but in a loop, it consistently chooses the same thing. For example, if I tell it to do this twice, both results are the same)

Here's the updated code.

http://pastebin.com/e5XeEbyX
I see. The value of "random" Never changes, because it is initialized outside of the loop.

Move these -
1
2
random = (rand() % (100) + 1);
	gold = (random * 5);


And put them at the top of the for-loop -

1
2
3
4
5
6
7
8
for (int x = 0; x < number; x++)
	{
		
		random = (rand() % (100) + 1);
		gold = (random * 5);

		cout << "You rolled " << random << endl;
		if (random >= 1 & random <= 20)


That will fix it :)

Also please change it to int main() and not int _tmain()
Last edited on
You're a wizard.

Thank you! I've been struggling with this problem for a bit.
You're very welcome. Please next time only post in one forum section :) goodluck to you.
Topic archived. No new replies allowed.