online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code    Language
/* Deck of Cards program created using a class called Cards to store card info and a Vector called Deck to hold all 52 cards. This program will include functions to shuffle the cards in the deck and deal the top card of the deck. */ #include <iostream> #include <cstdlib> //using this for some randomness when shuffling the deck. #include <ctime> //used for a seed of randomness when shuffling the deck. #include <vector> using namespace std; //global variables int choice = 0; //used for menu inputs bool done = false; //used for menu looping //Card class class Card { public: string name; //Ace, king, etc. string suit; //Hearts, Spades, etc. }; //Deck Class class DeckOfCards { public: DeckOfCards(); //constructor, initializes deck with card information. void shuffle(); //shuffles the cards in the deck Card dealCard(); //Returns the top card of the deck bool moreCards(); //Checks if there are more cards in the deck. int deckSize(); //Returns the number of remaining cards in the deck. private: vector<Card> deck; //Vector that holds all 52 cards. }; //Member functions for the Deck of Cards class, including the constructor DeckOfCards::DeckOfCards() { cout << "==Initializing Deck==" << endl; //nested for loops to generate card info and put it in the deck. Card filler; //temporary card object to fill the deck vector. for (int i = 0; i < 4; i++) { for (int j = 0; j < 13; j++) { switch (i) { case 0: filler.suit = "Spades"; break; case 1: filler.suit = "Hearts"; break; case 2: filler.suit = "Clubs"; break; case 3: filler.suit = "Diamonds"; break; } switch (j) { case 0: filler.name = "Ace"; break; case 1: filler.name = "King"; break; case 2: filler.name = "Queen"; break; case 3: filler.name = "Jack"; break; case 4: filler.name = "10"; break; case 5: filler.name = "9"; break; case 6: filler.name = "8"; break; case 7: filler.name = "7"; break; case 8: filler.name = "6"; break; case 9: filler.name = "5"; break; case 10: filler.name = "4"; break; case 11: filler.name = "3"; break; case 12: filler.name = "2"; break; } deck.push_back(filler); } } } void DeckOfCards::shuffle() { cout << "==Shuffling Deck==" << endl; vector<Card> temp; int halfDeck = ((deck.size() + 1) / 2); //using this to avoid hardcoding a value for half the deck's size in the shuffling for loop. //using pseudo randomness to decide how many times to shuffle the deck. srand((int)time(0)); int times = (rand() % 50)+1; //shuffling the deck for a random (1-50) number of times. for (int z = 0; z < times; z++) { //somewhat simulating the randomness of the bridge shuffling technique by dividing the deck in half and putting one or two cards from each side on top of the stack. AKA middle out shuffling. srand(times); for (int x = 0; x < halfDeck; x++) { int order = (rand() % 100) + 1; if (order % 2 != 0) { temp.push_back(deck[x]); temp.push_back(deck[x + halfDeck]); } else { temp.push_back(deck[x + halfDeck]); temp.push_back(deck[x]); } } deck.swap(temp);//deck takes on all of the values that were just randomly shuffled into the temporary vector. temp.clear();//clear resets the size of the temporary vector to 0 so it can be reused in the next shuffling operation. } } //removes top card of deck and returns the information. Card DeckOfCards::dealCard() { if (moreCards()) { Card drawnCard = deck[0]; //temporarily retain top card information deck.erase(deck.begin()); //erase top card from deck return drawnCard; //returns the top card } else { Card nullCard; cout << "There aren't anymore cards in the deck!" << endl; return nullCard; } } //checks to see if there are more cards to draw from the deck and returns a true or false. bool DeckOfCards::moreCards() { if (deck.size() != 0) { return true; } else { return false; } } int DeckOfCards::deckSize() { return deck.size(); } int useDeck() //This function is used by the main menu to run the DeckOfCards class functions. { Card currentCard; DeckOfCards deck; deck.shuffle(); while (!done) { cout << "\nWhat would you like to do?\n1-Draw a Card 2-Count Deck 0-Quit" << endl; cin >> choice; switch (choice) { case 0: done = true; break; case 1: currentCard = deck.dealCard(); cout << "This card is the " << currentCard.name << " of " << currentCard.suit << endl; break; case 2: if (deck.moreCards()) cout << "There are " << deck.deckSize() << " cards left in the deck.\n" << endl; else cout << "There aren't anymore cards in the deck!\n" << endl; break; default: cout << "That isn't a valid option.\n\n" << endl; break; } } return 0; } int main() { while (!done) { //Prompting user input. cout << "Thank you for choosing to use this virtual deck of cards!\n" << "Enter 1 to start drawing cards or 0 to close the program\n" << endl; //prompting an initial interaction cin >> choice; switch (choice) { case 0: done = true; break; case 1: useDeck(); break; default: //This loops the initial menu untl they choose to interact with the deck or close the program. cout << "That's not a valid option.\n\n" << endl; break; } } return 0; }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue