-2

I have a program here which returns how many shuffles in a deck it takes to find a card of each suit (hearts,diamonds etc..)

My problem is that, for some reason I keep getting numbers outputting . I am not sure why this is happening but I believe it is because of me seeding the randomizer. I would appreciate any feedback

cards.cpp
#include "cards.h"
#include <iostream>
#include <time.h>
#include <string>


int pickRandomCard() {

    int v1 = rand() % Constants::CARD_COUNT;
    std::cout << v1;
    return v1;

}
// return an enum representing the Rank of the card index given.
Rank getRank(int cardIndex) {

    Rank rankIndex = static_cast<Rank>(cardIndex);
    return rankIndex;
}
// return an enum representing the Suit of the card index given.
Suit getSuit(int cardIndex) {
    Suit suitIndex = static_cast<Suit>(cardIndex);
    return suitIndex;
}
// return true if all the Booleans in the given array are true, note we have to pass in 
//the length since our array “decays” to a pointer.
bool allSuitsPicked(const bool suitsPicked[], int length) {

    bool checkIfAllTrue=true;
    for (int i = 0; i < length; i++) {
        if (suitsPicked[i] == false) {
            checkIfAllTrue = false;
        }
    }


    return checkIfAllTrue;
}
// Return an int representing the number of card picks it takes to cover 4 suits.
// If verbose is true, generate output to show cards picked and pick count. 
// This is the function that does all the work behind solving the problem (including// sending output to the console).
// This function should use an array of Boolean values to represent the suits that
// have been picked already, and make use of allSuitsPicked() to test if complete.



int getPickCountNeededForFourSuits(bool verbose = true) {

    //
    int numberFoundWithSuit[4] = {};






    int pickCount = 0;


    //array of booleans values to represent the suits that have been already picked
    bool suitsPickedAlready[4] = { false,false,false,false };
    //initialzing our boolean array to use as a argument in our while loop
    bool allpicked = allSuitsPicked(suitsPickedAlready,4);
    //this should always run 
    while (allpicked == false) {
        //we are getting a random number between 0-4, to represent our suit

        int cardNumber = pickRandomCard()/13;

         //we are getting a random number from 0-13 to represent our rank
        int cardNumber2 = pickRandomCard()%13;


        //now we are getting a random suit using our random number
        Suit newSuitGenerated = getSuit(cardNumber);
        Rank newRankGenerated = getRank(cardNumber2);

        int valueOfSuit;

        switch (newSuitGenerated) {
        case Suit::Spades:
            valueOfSuit = 0;
            break;
        case Suit::Hearts:
            valueOfSuit = 1;
            break;
        case Suit::Diamonds:
            valueOfSuit = 2;
            break;
        case Suit::Clubs:
            valueOfSuit = 3;
            break;
        }
        int valueOfRank;
        //"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Jack", "Queen", "King"
        switch (newRankGenerated) {
        case Rank::Ace:
            valueOfRank = 0;
            break;
        case Rank::Two:
            valueOfRank = 1;
            break;
        case Rank::Three:
            valueOfRank = 2;
            break;
        case Rank::Four:
            valueOfRank = 3;
            break;
        case Rank::Five:
            valueOfRank = 4;
            break;
        case Rank::Six:
            valueOfRank = 5;
            break;
        case Rank::Seven:
            valueOfRank = 6;
            break;
        case Rank::Eight:
            valueOfRank = 7;
            break;
        case Rank::Nine:
            valueOfRank = 8;
            break;
        case Rank::Ten:
            valueOfRank = 9;
            break;
        case Rank::Jack:
            valueOfRank = 10;
            break;
        case Rank::Queen:
            valueOfRank = 11;
            break;
        case Rank::King:
            valueOfRank = 12;
            break;
        }
        //if that suit we generated has not already been found, we will add it to our array 
            if (suitsPickedAlready[valueOfSuit] == false) {
                suitsPickedAlready[valueOfSuit] = true;
                //adding that number to our array
                numberFoundWithSuit[valueOfSuit] = valueOfRank;

                std::cout <<Constants::SUITS[valueOfSuit] << " of ";
                std::cout << Constants::RANKS[valueOfRank] << '\n';
            }








        //re calling the while loop test argument to see if we have a full array of suits found
        pickCount++;
        allpicked = allSuitsPicked(suitsPickedAlready, 4);
}


    if (verbose == true) {


        std::cout <<"Number of picks: ";
        // If verbose is true, generate output to show cards picked and pick count. 
        return pickCount;
    }

}
cards.h
#pragma once
#include <string.h>
#include <string>
#include <string>

enum class Suit{ Spades, Hearts, Diamonds, Clubs };// this will need to be filled out
enum class Rank{ Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine,Ten, Jack, Queen, King };// this will need to be filled out


namespace Constants {
    constexpr int CARD_COUNT{ 52 }; 
    constexpr int NUM_RANKS{ 13 }; 
    constexpr int NUM_SUITS{ 4 };
    // These string arrays will simplify the task of printing out the card values.
    // We don’t need to use a switch statement to find the string representation
    // of an enum.  We can cast the enum as an int to index these arrays.

    const std::string SUITS[NUM_SUITS] { "Spades", "Hearts", "Diamonds", "Clubs" };// initialize & match with enums
    const std::string RANKS[NUM_RANKS] { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten","Jack", "Queen", "King" };// initialize & match with enums
}

// FUNCTION PROTOTYPES// pick a random card from the deck (return a random # between 0-51)
int pickRandomCard();
// return an enum representing the Rank of the card index given.
Rank getRank(int cardIndex);
// return an enum representing the Suit of the card index given.
Suit getSuit(int cardIndex);
// return true if all the Booleans in the given array are true, note we have to pass in 
//the length since our array “decays” to a pointer.
bool allSuitsPicked(const bool suitsPicked[], int length);
// Return an int representing the number of card picks it takes to cover 4 suits.
// If verbose is true, generate output to show cards picked and pick count. 
// This is the function that does all the work behind solving the problem (including// sending output to the console).
// This function should use an array of Boolean values to represent the suits that
// have been picked already, and make use of allSuitsPicked() to test if complete.
int getPickCountNeededForFourSuits(bool);
Source.cpp

#include<stdio.h>
#include<string>
#include <iostream>
#include "cards.cpp"
#include<time.h>


int main() {
    srand(time(0));
    srand(static_cast<unsigned int>(time(0)));
    /*Write a single-file program (named main.cpp) that reads two separate integers from the user, adds them together, and then outputs the answer. The program should use three functions:

A function named “readNumber” should be used to get (and return) a single integer from the user.
A function named “writeAnswer” should be used to output the answer. This function should take a single parameter and have no return value.
A main() function should be used to glue the above functions together.*/



    int x = getPickCountNeededForFourSuits(true);
    std::cout << x;




}
Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56
RocStream
  • 67
  • 7
  • 1
    If you have a theory, then test that theory yourself. If you have a problem with actual output being different from expected output then _show both actual and expected output_. – paddy Oct 02 '19 at 03:17
  • 1
    You would probably have gotten your answer by now if you used a debugger. Set breakpoints on each line that mentions `std::cout`. When you hit a breakpoint, examine the value being sent to `std::cout`. When you find numbers, you have your culprit. – JaMiT Oct 02 '19 at 03:18
  • 1
    Can you give an example of the printout you are getting that you don't want? –  Oct 02 '19 at 03:28
  • Even if I remove the std::cout I keep getting random inputs.. its like 12234827 then my output, followed by more 1287318236 then my next cout – RocStream Oct 02 '19 at 03:55
  • Please turn the code puzzle into a [mre]. – Yunnosch Oct 02 '19 at 06:09

1 Answers1

2

I made a MRE (see my comment) from your code (please do that yourself in the future).
I could reproduce your observation from the original question:

4629Clubs of Four
4535138174Hearts of Five
622Spades of Ten
2739Diamonds of Ace
Number of picks: 6

I could not reproduce your observation from comment
"Even if I remove the std::cout I keep getting random inputs.. its like 12234827 then my output, followed by more 1287318236 then my next cout",
because when I delete the std::cout << v1; from int pickRandomCard(), then I get:

Clubs of Eight
Spades of Seven
Hearts of Jack
Diamonds of Jack
Number of picks: 4

So, the solution is

  • use the -Wall -Wextra -pedantic option or stricter for compiling and fix the warnings
    (not really required, but generally a good idea)
  • use your debugger, like JaMiT proposed
    How to debug using gdb?
    How to debug in Codeblocks?
  • delete the number-outputting line
  • double-check that the code you show here is actually the code you use at home
  • find the difference between shown code and the code at home
  • do the same again, one additional time should be enough, otherwise repeat

(Hello fellow StackOverflow users. If you think that my answer means the question should be closed for not being reproduceable, then I won't contradict. I just thought that the pproblem in the question itself can be reproduced. Feel free to close-vote and delete.)

Yunnosch
  • 26,130
  • 9
  • 42
  • 54