Here I am creating a program where I can add marbles to a bag. I am also assigning a random name to the number of marbles that I enter, hence why I have an array of names. The size of my name array is 10 and holds ten name values. When I go to add marbles to the bag, my code loops through and say I add 5 marbles to the bag, instead of going: 0-----Andrew, 1-----Dustin, and so on up to 5. it actually loops I think 6 different times.
I have tried playing around with my for loops and arrays.
void addmarbles(){
int x;
cout<< "how many random marbles would you like to enter?" << endl;
cin >>choice;
srand(time(0));
for(int i=0; i<arraysize; i++) {
for(x = 0; x<=choice; x++){
int index = rand() % arraysize;
string swap = names[i];
names[i] = names[index];
names[index] = swap;
cout<< x<< "------------" << names[index] <<endl;
}
}
bag += choice;
cout << "The current amount of marbles in the bag is: "<< bag <<endl;
main();
}
expected the output say if I add 5 marbles to be:
0------------andrew 1------------vicki 2------------josh 3------------branden 4------------ryan 5------------dustin
but instead its this:
0------------andrew 1------------andrew 2------------vicki 3------------josh 4------------josh 5------------ryan 0------------vicki 1------------vicki 2------------ryan 3------------josh 4------------dustin 5------------mark 0------------branden 1------------justin 2------------smitty 3------------branden 4------------justin 5------------carter 0------------ryan 1------------josh 2------------justin 3------------andrew 4------------mark 5------------justin 0------------vicki 1------------justin 2------------carter 3------------andrew 4------------vicki 5------------andrew 0------------vicki 1------------branden 2------------vicki 3------------vicki 4------------andrew 5------------dustin 0------------justin 1------------justin 2------------mark 3------------mark 4------------andrew 5------------ryan 0------------branden 1------------branden 2------------mark 3------------ryan 4------------mark 5------------branden 0------------carter 1------------justin 2------------dustin 3------------justin 4------------mark 5------------dustin 0------------justin 1------------josh 2------------vicki 3------------carter 4------------justin 5------------andrew
Aucun commentaire:
Enregistrer un commentaire