so I am making a program where you can add marbles to a bag. You chose what color you want, then the number of marbles you want to add, and then if that color marble is either cool, boring, or weird. so if you want to add 10 blue marbles, and then you chose cool. You will have ten total marbles and ten marbles that are blue and cool. I go to remove a random marble from the marbles that are stored already after I add. For some reason, it isn't working at all. I attempt to get a random number and assign that random number to a description of a current marble that has already been added. Say if the random number generated is 1 it will remove a " red cool marble " I obviously can't remove a marble that hasn't been added so I added a DO while loop, which basically loops back through the random number generator until it finds a number that satisfies the criteria. Here is my code, any thoughts? :
I tried changing the position of my Do while loop.
void remove() {
srand(time(0));
do {
randomnum = (rand () % max) + 1;
if (randomnum == 1 && redcool > 0) {
red--;
redcool--;
cout << "The random marble removed was a red and cool marble" <<endl;
}
else if (randomnum == 2 && redweird > 0) {
cout << "The random marble removed was a red and weirdl marble" <<endl;
red--;
redweird--;
}
else if (randomnum == 3 && redboring > 0) {
cout << "The random marble removed was a red and boring marble" <<endl;
red--;
redboring--;
}
else if (randomnum == 4 && greencool > 0) {
cout << "The random marble removed was a green and cool marble" <<endl;
green--;
greencool--;
}
else if (randomnum == 5 && greenweird > 0) {
cout << "The random marble removed was a green and weird marble" <<endl;
green--;
greenweird--;
}
else if (randomnum == 6 && greenboring > 0) {
cout << "The random marble removed was a green and boring marble" <<endl;
greenboring--;
green--;
}
else if (randomnum == 7 && greenboring < 0) {
cout << "The random marble removed was a green and cool marble" <<endl;
greencool--;
green--;
}
else if (randomnum == 8 && bluecool >0) {
cout << "The random marble removed was a blue and cool marble" <<endl;
blue--;
bluecool--;
}
else if (randomnum == 9 && blueweird > 0) {
cout << "The random marble removed was a blue and weird marble" <<endl;
blue--;
blueweird--;
}
else if (randomnum == 10 && blueboring > 0) {
cout << "The random marble removed was a blue and boring marble" <<endl;
blue--;
blueboring--;
}
}while (randomnum == 1 && redcool > 0 || randomnum == 2 && redweird >0 || randomnum == 3
&& redboring > 0 || randomnum == 4 && greencool > 0 || randomnum == 5 && greenweird >0 || randomnum == 6
&& greenboring > 0 || randomnum == 7 && bluecool > 0 || randomnum == 8 && blueweird >0 || randomnum == 9
&& blueboring > 0);
bag--;
the expected result would say I add ten marbles that are blue and cool. When I go to remove a random marble it will then decrement a blue and cool marble because that is the only values that are in the bag. currently, nothing is even being removed.
Aucun commentaire:
Enregistrer un commentaire