如何停止程序输出时间,这使程序难以阅读

时间:2019-10-02 02:44:03

标签: c++

我这里有一个程序,该程序返回在甲板上找到每套西装(心脏,钻石等)所需的洗牌次数。

我的问题是,由于某种原因,我一直在输出数字。我不确定为什么会这样,但是我相信这是因为我为随机化器植入了种子。我将不胜感激

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;




}

1 个答案:

答案 0 :(得分:2)

我从您的代码中制作了MRE(请参阅我的评论)(请以后自己做)。
我可以从原始问题中复制您的观察结果:

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

我无法从评论中复制您的观察
“即使删除了std :: cout,我仍然会得到随机输入。它的输出像是12234827,然后是我的输出,接着是更多的1287318236,然后是我的下一个cout”,
因为当我从std::cout << v1;删除int pickRandomCard()时,我得到:

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

所以,解决方法是

  • 使用-Wall -Wextra -pedantic选项或更严格的选项来编译和修复警告
    (不是必需的,但通常是个好主意)
  • 使用调试器,如建议的JaMiT
    How to debug using gdb?
    How to debug in Codeblocks?
  • 删除数字输出行
  • 再次检查您在此处显示的代码是否实际上是您在家中使用的代码
  • 找到显示的代码和在家中的代码之间的区别
  • 再次执行相同操作,应该再增加一次时间,否则请重复

您好,StackOverflow的用户。如果您认为我的回答意味着该问题因不可复制而应该被关闭,那么我不会提出抵触。我只是认为问题本身中的问题可以被复制。随时进行关闭投票和删除。