0

Pretty much creating a card game for class. Not looking for straight up answers but a better understanding to learn the material of why im getting the problem i am. Thanks for any help i can get. Heres the problem:

Create a card game called flash card using struct data structure when each player draws three cards for random numbers (1-4) as Diamond, Spade, Clubs and Hearts respectively and another set of random numbers of card value 1-13. The cardScore is defined as highest (say cardScore =100 for three A s’ (cardValue 1), followed by three Jacks (cardValue 11) (say cardScore 90), followed by three Kings (cardValue 13)(say cardScore =80) and then three Queens (cardValue 12)(say cardScore = 70). Other than the above said combinations the cardScore is defined by the sum of the cardValue of each draw. The player which has the highest score after three draws win. Display the cards drawn by each player and the cardScore and the winner

#include "pch.h"
#include <iostream>
#include <string>
#include <stdlib.h> 
#include <math.h>
using namespace std;
class Values
{
public:
    string cardFace;
    int cardValue;
    int cardFaceRng;
    int cardValueRng;
private:


};

//declaring functions
void setCardFaceRandom();
void setCardValueRandom();
void setCardFace();
void setCardValue();
int main()
{
    Values v;



    for (int i = 0; i < 3; i++) 
    {

        v.cardFaceRng = rand() % 3 + 1;
        v.cardValueRng = rand() % 14 + 1;
        //setCardFaceRandom();
        //setCardValueRandom();
        setCardFace();
        setCardValue();
            cout << v.cardFace << " " << v.cardValue << endl;
    }
    system("pause");
    return 0;
}

void setCardFace() {
    Values v;
        switch (v.cardFaceRng)
                            {
                                    case 0:
                                        v.cardFace = "Diamond";
                                        break;
                                    case 1:
                                        v.cardFace = "Spade";
                                        break;
                                    case 2:
                                        v.cardFace = "Club";
                                        break;
                                    case 3:
                                        v.cardFace = "Hearts";
                                        break;
                            }
        cout << v.cardFace;

}


void setCardValue() {
    Values v;
    switch (v.cardValueRng)
    {
    case 0:
        v.cardValue = 2;
        break;
    case 1:
        v.cardValue = 3;
        break;
    case 2:
        v.cardValue = 4;
        break;
    case 3:
        v.cardValue = 5;
        break;
    case 4:
        v.cardValue = 6;
        break;
    case 5:
        v.cardValue = 7;
        break;
    case 6:
        v.cardValue = 8;
        break;
    case 7:
        v.cardValue = 9;
        break;
    case 8:
        v.cardValue = 10;
        break;
    case 9:
        v.cardValue = 11;
        break;
    case 10:
        v.cardValue = 12;
        break;
    case 11:
        v.cardValue = 13;
        break;
    case 12:
        v.cardValue = 14;
        break;

    }
    cout << v.cardValue;

}


void setCardFaceRandom()

    {
        Values v;

             v.cardFaceRng = rand() % 3 + 1;

    }



    void setCardValueRandom()

    {
        Values v;

            v.cardValueRng = rand() % 14 + 1;

    }
datstache
  • 13
  • 1
  • 2
    -858993460 = 0xCCCCCCCC which means [you've accessed uninitialized memory](https://stackoverflow.com/q/370195/995714) – phuclv Feb 22 '19 at 04:43
  • 1
    Anytime you see a huge, bizarre number like that, turn it into hexadecimal and see if it matches a known [debugging magic number](https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_debug_values) – user4581301 Feb 22 '19 at 05:08

1 Answers1

0

Using only one of the functions as an example. All functions have the same problem:

void setCardFace() {
Values v;
    switch (v.cardFaceRng)

This creates a new local variable in this function called v, and default-constructs it. Since the Values class does not have an explicit constructor, all class members are default-constructed, and for int members of this class, they're not initialized to anything. They're random garbage. So this v.cardFaceRng is random garbage, and from this point on, it's undefined behavior.

The v object in this function, setCardFace() has absolutely nothing to do with the v object you declared and initialized in your main(). All objects declared inside functions are local objects to that function, that only exist in that function, and are destroyed when the function returns; and different functions can have different local objects that have the same name, and are, otherwise, completely independent of each other.

In order for this setCardFace() class to use the object you declared and initialized in your main() -- as your obvious intent is here -- you need to explicitly pass it as a parameter to this function, and all other functions you declared -- and preferably pass it by reference.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • I appreciate the reply an i understand now why its doing that. would you be able to show me an example of referencing a class as a parameter and passing them to a function? – datstache Feb 22 '19 at 05:04
  • 1
    You should be able to find plenty of examples in your C++ book. If you don't understand something in your C++ book, you can always post another question, with specific details of what exactly you do not understand. – Sam Varshavchik Feb 22 '19 at 05:06
  • Awesome thank you for the input i was able to take all the garbage values out and properly set reference like so " Values vh; Values *v = &vh; " and passed this to all functions! – datstache Feb 22 '19 at 05:14