Loops, looping more than they should.
I am working on this program to deal 13 random cards, and for some reason half the time one of my loops is looping more than they should... i cant work out why...
its the do loop in there...
Code:
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
start:
system("cls");
srand ( (unsigned)time ( NULL ) );
int var[200];
for (int i = 0; i < 199; i++ )
var[i] = ( "%d ", rand() % 100 );
string getSpade[13] = {"A", "2", "3", "4", "5" ,"6", "7", "8", "9", "10", "J", "Q", "K"};
random_shuffle(getSpade, getSpade + 13);
string getHeart[13] = {"A", "2", "3", "4", "5" ,"6", "7", "8", "9", "10", "J", "Q", "K"};
random_shuffle(getHeart, getHeart + 13);
string getDiamond[13] = {"A", "2", "3", "4", "5" ,"6", "7", "8", "9", "10", "J", "Q", "K"};
random_shuffle(getDiamond, getDiamond + 13);
string getClub[13] = {"A", "2", "3", "4", "5" ,"6", "7", "8", "9", "10", "J", "Q", "K"};
random_shuffle(getClub, getClub + 13);
int nextSpade = 0, nextHeart = 0, nextDiamond = 0, nextClub = 0;
string pOneSpade[13], pOneHeart[13], pOneDiamond[13], pOneClub[13];
int f=0,i=0;
do
{
if ((var[f]%2 != 0) && (nextSpade <= 12))
{
pOneSpade[nextSpade] = getSpade[nextSpade];
nextSpade++; i++;
}
f++;
if ((var[f]%2 != 0) && (nextHeart <= 12))
{
pOneHeart[nextHeart] = getHeart[nextHeart];
nextHeart++; i++;
}
f++;
if ((var[f]%2 != 0) && (nextDiamond <= 12))
{
pOneDiamond[nextDiamond] = getDiamond[nextDiamond];
nextDiamond++; i++;
}
f++;
if ((var[f]%2 != 0) && (nextClub <= 12))
{
pOneClub[nextClub] = getClub[nextClub];
nextClub++; i++;
}
f++;
} while (i <= 12);
// Print player one's cards.
cout << "Player One \n"
<< "S: ";
for (int i = 0; i <= nextSpade-1; i++)
cout << pOneSpade[i] << " ";
cout << endl;
cout << "H: ";
for (int i = 0; i <= nextHeart-1; i++)
cout << pOneHeart[i] << " ";
cout << endl;
cout << "D: ";
for (int i = 0; i <= nextDiamond-1; i++)
cout << pOneDiamond[i] << " ";
cout << endl;
cout << "C: ";
for (int i = 0; i <= nextClub-1; i++)
cout << pOneClub[i] << " ";
cout << endl;
// Play again option.
char ex;
cout << "Play again? (y or n)";
te:
cin >> ex;
switch (ex)
{
case 'y': goto start; break;
case 'n': system("cls"); break;
default: goto te; break;
}
return EXIT_SUCCESS;
}