-
Make it stop!
Ok, ive been slaving over this for hours, and it finally works, sorta. Ive written a program using the cards class (cards.h) that deals 5 cards 10000 times and spits back out the number of flushes that were dealt. Say there were 18, instead of printing 18 it fills the screen with 18's and keeps going. I cant get it to stop! HEEEELP!
Here's the code (keep in mind , i am a beginner).
#include "cards.h"
void printcard(int suit);
int hearts = 0;
int diamonds = 0;
int spades = 0;
int clubs = 0;
int counter = 0;
int shuffle = 0;
//precondition : suit is in the range 1..4,
//post condition: prints the suit of the card
void main()
{
cards deck; // declare a Cards object
int card; // three digit number representing the card
int suit; //1 = hearts, 2 = diamonds, 3 = clubs, 4 = spades
while (shuffle <=10000)
{
deck.shuffle(); // shuffle the deck;
// deal five cards
for (int i = 1; i <= 5; i++)
{
card = deck.deal();
suit = card/100;
printcard(suit);
}
cout << counter << endl;
} ///this closes while
} ///added to close main
void printcard(int suit)
{
if (suit == 1) {
hearts++;
} else if (suit ==2) {
diamonds++;
} else if (suit ==3) {
clubs++;
} else {
spades++;
}
if (hearts ==5) {
counter++;
}
if (diamonds ==5) {
counter++;
}
if (clubs ==5) {
counter++;
}
if (spades ==5) {
counter++;
} ///close if
} // closes printcard
-
First don't use void main, use int main. Second, there doesn't appear to be any return value to terminate the program - put in return 0; the line above where you have commented "// added to close main".
See if this works. If not, then could you post your cards.h file as well?
-
No dice, same thing. Here's the card.h file as requested
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
class cards
{
private:
int deck[53];
int card;
public:
cards();
void shuffle();
int deal();
int remaining();
};
cards::cards()
{
int rn;
int temp;
srand(clock());
for (int i = 1; i <= 13; i++)
{
deck [i] = 100+i;
deck[i+13] = 200+i;
deck[i + 26] = 300+i;
deck[i + 39 ] = 400+i;
}
for (int i = 1; i <= 52; i++)
{
rn = rand()%52 + 1;
temp = deck[i];
deck[i] = deck[rn];
deck[rn] = temp;
}
card = 0;
}
void cards::shuffle()
{
for (int i = 1; i <= 52; i++)
{
int rn = rand()%52 + 1;
int temp = deck[i];
deck[i] = deck[rn];
deck[rn] = temp;
}
card = 0;
}
int cards::deal()
{
card ++;
return deck[card] ;
}
int cards::remaining()
{
return (52-card);
}