Storing data from loop to array?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int getcard()//function to call to main program
{ 
    srand((unsigned)time(0)); 

	int x;
	string mix[10];
	string h;
	do
	{
   for(int ran= 0; ran<1; ran++)//this loop randomize the array
	{ 
		 x= rand() % 18;
			 h = master[x];
		cout << h << endl; //print randomize data
    } 

	mix[10] = h;//store data from "h" to mix
	} 
	while (false);
 
	return 0;
}



Hi, i'm new to c++ and my question is how do i store data from a loop cycle into a array, then print it?
Do i need to make another loop? if so how do i go about that? also if what i ask is possible, when every time i re-run the compiler will the array above get re-written or erased(hope it does)?
basic as possible plz if not plz explain thank you
Any variables used by a program are lost once the program completes execution. Technically the data is still in the memory, it's just that the memory is no longer held in reserve by the program so it is free to be accessed by any other program.

What your for loop there is doing is assigning the value contained in master[x] to h. Have you already assigned values to all elements of master[]? If so this loop gets a random element of master[] and assigns it to h which you then output, then assigns that value to the 10th element of mix[]. Since you initialize ran to 0, and use ran < 1 as your test expression, the loop will only run once ( for every time you call getcard() that is.) This may be exactly what you want to do, but I feel like it's not. If you can describe what your intentions and thoughts are for this function we can prolly offer better advice. If you only need to get one card you don't need to use a for loop at all. Also note, since you declared x, mix[] and h inside the function they only have scope inside the function. Meaning that after the function returns those variables no longer exist, much like variables declared in main no longer exist when the program is finished. And lastly, your do while tests while (false) meaning that, once again, the whole block will only execute once, so you have 2 redundant loops.
Topic archived. No new replies allowed.