How would I condense this into loops so I don't have to repeat all this code? And no I don't want to call a function. Just loop :) Thanks. I am just not sure how to without having the same result. I have been trying for hours.
Loops are all about generalizing a pattern.
Look at what each if/else is doing. Each of them is doing: r = rand() % 2; and then checking results. You also know that you want these checked 12 times. Since the loop is limited by a counting value this sounds like a good candidate for a 'for' loop.
Just based on that you could have this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
srand(time(0)); //seed the random number
for(int i=1; i<=12; i++) //12 iterations starting from 1
{
r = rand() % 2;
if (r == 0)
{
selection = selection + 0.5;
cout << selection << endl;
}
else
{
selection = selection - 0.5;
cout << selection << endl;
}
}
As it turns out the top loop can be simplified even more by adjusting the output from the random number to fit ur needs:
1 2 3 4 5 6 7 8
srand(time(0)); //seed the random number
//r = rand() % 2; //randomize either a zero or a one //uneeded
for(int i=0; i<12; i++) //12 iterations starting from 0
{
selection += (rand() % 2) - 0.5;
cout << selection << endl;
}
for (int i = 0; i < 12; i++)
{
int r = rand() % 2;
if (r == 0)
{
selection = selection + 0.5;
cout << selection << endl;
}
else
{
selection = selection - 0.5;
cout << selection << endl;
}
}
By the way, your if statements at the end won't do what you expect them to. Each clause is actually an assigment statement (=) rather than an "is equal" statement (==). Each assignement that is not 0 will evaluate to true, so userWinnings will be set to 500, 1000, 0, 10000, 0, 1000, 500 and 100 each time through.
Is this the 'selection' from the user ? or the adjusted selection from the +- 0.5 loop ? What do you want to happen if it does hit 10 or 0? Stop? Only count in the opposite direction ?
if the adjusted selection ever hits 0 or 10 it goes back to 0.5 or 9.5 respectively so it never goes under/over. just keep the adjusted selections always in between 0-10