you have to increment the index using ++i or i++ statrting from a base value. |
I would not say "have to".
1. The
num += 1
increments
num
by one, just like
++num
or
num++
does.
Granted, ++num is more common style than num+=1, but on some loops you might have to do num+=4 and some loops have no incrementing statement at all.
2. We have "starting base value" here.
1 2 3 4
|
cin >> num;
for (; num < max; num += 1) {
// something
}
| |
The tricky part is that it is from user, so there are two possibilities. The initial value of num could be
A) less than max, in which case the loop iterates
max-num
times
B) >=max, in which case loop iterates 0 times
Even in the case A, if I give "4", expecting to get four hands according to the prompts, the game will give 5-4, that is only one hand. The UI and logic do not match.
If the user really should decide, then:
1 2 3 4
|
cin >> num;
for (int hand=0; hand < num; ++hand) {
// something
}
| |
or, like seeplus had it:
1 2 3 4
|
cin >> num;
while ( num-- ) {
// something
}
| |
In that while loop the --num and num-- do produce different result.
If you want both user to decide and have an upper limit, then you can for example sanitize the input first:
1 2 3 4 5
|
cin >> num;
if ( num > max ) num = max;
while ( num-- ) {
// something
}
| |