-1

Forgive my ignorance. I'm still at the beginning of C++, and am having a hard time grasping the language, since it is my first.

How would I call the strings or int in a function into another function so I would be able to separate the different processes required for the program?

I'm really sorry if it's messy, from my understanding the code should work, but it is displaying nothing on the main.cpp file.

This is a full house probability checker. I'm supposed to run a bunch of rounds and deal out five cards and check if they are a full house. Please tell me what I can improve on, and what I lack in my understanding.

class.cpp

#include <iostream>
#include "Class.h"
#include <ctime>
#include <cstdlib>
#include <string>

using namespace std;

void shufflewell()
{
    srand(time(0));
    int deck[52];
    int i;

    // deck shuffling
    for(i=0; i<52; i++)
    {
        int j = rand() % 52;
        int temp = deck[i];
        deck[i] = deck[j];
        deck[j] = temp;
    }
}

void DisplayCard ()
{
    void shufflewell();
    int i;
    int deck[52];
    string suitnames[4]={"spades", "diamonds", "clubs", "hearts"};
    string ranknames[13]={"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
    int suitnumber = deck[i] / 13; // 0 - 3
    int rank = deck[i] % 13;
    cout << ranknames[rank] << " of " << suitnames[suitnumber] << endl;
    // Get the rank of the first 5 cards
    int R[5]; // = {4, 7, 6, 3, 5}; // rank of the first 5 cards
    int S[5];
    for(i=0; i<5; i++)
    {
        R[i] = deck[i]%13;
        S[i] = deck[i]/13;
    }
    cout << ranknames[R[i]] << " of " << suitnames[S[i]];
    return DisplayCard();
}

// Deals five random cards to player
void dealHand()
{
    for(int i = 0; i < 5; i++)
    {
       DisplayCard();
       cout << ranknames[R[i]] << " of " << suitnames[S[i]];
    }
}

main.cpp

#include <iostream>
#include <cstdlib>
#include "Class.h"

using namespace std;

int main()
{
    cout << "Full house probabilty checker";
    void DisplayCard();
    return 0;
}

Class.h

#ifndef Class.h
#define Class.h

#include <iostream>
#include <string>

using namespace std;

class cards
{
    public:
        int i;
        int deck[52];
        std::string suitnames;
        std::string ranknames;
        void shufflewell();
        void DisplayCard();
        void dealHand();

};

#endif // Class
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • `void shufflewell();` is a declaration. It does not call the function. So is `void DisplayCard();` – drescherjm Nov 07 '22 at 21:00
  • 2
    Sounds like you're looking for how to use arguments/parameters to your functions. You should consult whatever learning resource you are using for that. I would also avoid `using namespace std;` in your header files. – Rogue Nov 07 '22 at 21:00
  • 1
    I don't understand what you are asking. But in `main()`, `void DisplayCard();` is a function *declaration*. To actually *call* the function, drop the `void`, eg: `DisplayCard();` However, all of the methods in `card.cpp` need to be prefixed with the class name, eg: `void cards::shufflewell() { ... }`, and since they are class methods, not standalone functions, `main()` needs a `cards` object to call the methods on, eg: `cards c; c.DisplayCard();` Also, in `DisplayCard()`, calling `return DisplayCard();` will make `DisplayCard()` call itself, thus running an endless recursion loop. – Remy Lebeau Nov 07 '22 at 21:01
  • Warning: C++ is a fairly complicated language, one of the most complicated languages in general use. It is next to impossible to learn it properly without careful reading of good reference materials. – user4581301 Nov 07 '22 at 21:06
  • 1
    Get yourself some [good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to learn from. – Remy Lebeau Nov 07 '22 at 21:07
  • And those C++ books will explain that if you get an error from your compiler complaining about an unknown object called `deck`, and you don't understand why because `deck` is defined in one of the function you wrote, then adding the same declaration into all the other functions might result in the error message going away, but the resulting code is completely broken, and will not work at all. C++ is just too complicated, and any attempt to guess what the right C++ syntax would be, even if the guess compiles, will always end in tears. – Sam Varshavchik Nov 07 '22 at 21:13

1 Answers1

0

Since C++ is your first language I would suggest reading up on on proper syntax form. Things like calling functions, creating classes and declaring member functions to name a few.

In C++ class member functions are conventionally implemented using a declaration:

// class.h
class cards
{
    public:
        /* ... */
        void DisplayCard();

};

Definition:

// class.cpp

#include "class.h"

void cards::DisplayCard()
{
     /* ... */
}

And get called like this:

class_Instance.DisplayCard();

In addition, you are calling DisplayCard from within itself:

void DisplayCard()
{
     /* ... */
     return DisplayCard(); 
}

Which causes a stackoverflow - function being executed an exceeding amount of times.

Here are a few resources on how classes and objects are created and used in C++:

Sup3rlum
  • 121
  • 4
  • The first example at geeksforgeeks starts with `#include ` `using namespace std;` – Ted Lyngmo Nov 07 '22 at 21:21
  • Going as far as to say it should not be linked to or recommended for any reason is a stretch. OP is looking for the very basics, which that page did a job of explaining to some degree. Regardless, I've changed it for a different website which is also easier to look through. – Sup3rlum Nov 07 '22 at 21:26