There are a number of problems initially.
Code:
srand((unsigned)time(0));
You are probably seeding too much in this function when you should be doing it somewhere else. Each time you seed, you restart the random number generator, which will probably result in not very random numbers. You might want to read this page: Eternally Confuzzled - Using rand()
Code:
for(int index= 0; index<10; index++)
{
x= rand() % 18;
h = master[x];
cout << h << endl;
}
mix[10] = h;
x is not chosen properly. It could be that x holds a value bigger than the acceptable range of master indexes. And there is no master[10]. Rather the size of the array comes from the possible indexes 0,1,2,3,4,5,6,7,8, and 9 being 10 numbers.
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?
It depends on when you want to display the data mostly.
Code:
string master[4] = { "alpha", "beta", "gamma", "delta" };
string s;
for (int i = 0; i < 4; i++ )
{
cout << "Enter new string: ";
cin >> s;
master[i] = s;
cout << i << ". " << master[i] << endl;
}
Something like that is as valid as a separate loop that just displays the data. It ultimately shouldn't matter if the index is randomly picked, the idea doesn't change.
Also I should tell you that do ... while (false); will only ever run once.
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)?
There are few guarantees in the area. Given modern computers chances are that the array will be in a different place each time you run the program thanks to address space layout randomization. And also, since you are using automatic memory, you really don't need to worry about that memory returning to the system after the program ends. It will be taken care of for you, automatically.