Hello,
This is my first post on these forums. I recently got in to programming (about two weeks ago).
I'm a civil engineer major but enjoy doing a lot of other non civil, engineering related projects on my free time.
As this is my first post, I just wanted to say, this website and the forums have taught me significantly more than my (for some reason) required semester of ANSI C ever taught me, and has compelled me to delve further in to the world of programming. I thank you all for this.
I began programming in c++ in order to produce, what I still consider to be, a "simple", turned based strategy game. Here is my 5th, maybe 6th iteration of the base of the code. I am trying to refine, and learn really what it is I am doing before I start adding too much to the code.
If you skipped that intro! I will now get to the point. I am trying to randomize an array of 5 integers that for now can range from 1-10. Some of the code may not make sense at this point because of plans I have for it in the future, but currently:
I DO NOT RETURN VALUES RANGING FROM 1-10, WHICH IS WHAT I WISH FOR MY VALUES TO BE LIMITED TO. Currently, my first two integers are consistently between 1-10 and the third is in the 10^(4 or 5) range and the last two are in the hundreds.
Can you please help me figure out what is wrong with my code? I would like 5 integers between 1-10 to be assigned to the array "move_1_0"... and 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
|
#include <cstdlib>
#include <iostream>
#include <time.h>
using namespace std;
int main()
{
int move_1_0 [5], reloop, i, y, n;
srand(time(NULL));
for(i=0;i<5;i++)
{
if (move_1_0[i] == 0) //move_<player>_<position> <1-5>
{
bool reloop = true;
while (reloop)
{
move_1_0[i] = rand()%10+1; //<-Shouldn't this confine my random numbers to 1-10?
for (y=0;y<5;y++)
{
if(move_1_0[i] != move_1_0[y])
{
reloop = false;
break;
}
/*in order to ensure no number shows up twice */
}
}
}
cout<<i+1<<") Move:"<<move_1_0[i]<<"\n"; //Should display the randomized numbers ranging from 1-10 in a list as 1-5?
}
system("PAUSE");
return EXIT_SUCCESS;
}
|
(I am using Dev C++)
...and double thanks if you can help!