O.k. guys. I've looked over and over this code and hoping maybe somebody can help me out. It's supposed to be a random card generator. You type in how many cards you want to draw and it deals them to you and prints, for example if you enter 3, "ace of diamonds, six of hearts, 4 of spades." Problem is when I type in a number it just skips on past printing anything and ends the program. I'm sure it's nothing more than a beginner problem but any help will be appreciated. Here it is:
int main()
{
int n, i;
srand (time(NULL)); //Set seed for randomizing.
while (1)
{
cout << "Enter number of cards to draw ";
cout << "(0 to exit): ";
cin >> n;
if (n == 0);
break;
for (i = 1; i <= n; i++)
draw_a_card();
}
return 0;
}
// Draw-A-Card Function
// Perform a card draw by getting a random 0-4 and
// a radom 0-12. Use these to index strings
// arrrays, ranks, and suits.
//
void draw_a_card()
{
int r; //Random index (0 thru 12) into
// ranks array
int s; // Random index (0 thru 3) into
// suits array
int card;
card = rand_0toN1(52); //Get random num 0 to 51
r = card % 13;
s = card / 13;
cout << ranks[r] << " of " << suits[s] << endl;
}
//Random 0-to-N1 Function
//Generate a random integer from 0 to N1.
//
int rand_0toN1(int n)
{
return rand() % n;
}
Remove that semi-colon. The semi-colon indicates the end of things to do if n==0, so in your code, when n==0, nothing is done, and then the code moves on to the next statement, which is break;, so break always gets exectued.
Wow. I have to say I never noticed that semi-colon had been placed there while I coded. I looked over this numerous times and never noticed that probably due to the fact I was thinking my function was possibly messed up somehow. Thank you very much.