My programming is displaying the alpha character rather than what I want?

I am trying to get the program to display the dealers hand, and then the players hand. This works. But when I call the hit() function, the dealers hand turns into "X (alpha)" and I don't know why. I am assuming that I am using the friend function wrong?

Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include "Player.h"
#include "Dealer.h"
using namespace std;

int main()
{
    cout << "Press H to hit or S to stay." << endl << endl;

    Player pstart;
    Dealer dstart;

    dstart.deal(); //displays the dealers hand just fine
    pstart.deal(); //displays the players hand just fine

    char hitstay;

    cin >> hitstay;
   if (hitstay == 'h')
        pstart.hit(); //this is where my problem occurs.  When it displays the dealers hand, it gives "X (alpha)", rather than "X (the num I want)".


    return 0;
}


Dealer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef DEALER_H
#define DEALER_H


class Dealer: public Player
{
    public:
        Dealer();
        friend class Player;

        void deal();
        void disphanddealer();
    protected:
    private:
        int card1, card2;
        char card1f, card2f;
};

#endif // DEALER_H 


Player.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef PLAYER_H
#define PLAYER_H

class Player
{
    public:

        Player();

        void deal();
        void disphand();
        void face();
        void hit();

    protected:
    private:
        int card1, card2;
        char card1f, card2f;

};

#endif // PLAYER_H 


Dealer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include "Player.h"
#include "Dealer.h"
using namespace std;

Dealer::Dealer()
{
}

void Dealer::deal()
{
        srand(time(0));

    card1 = (rand()%13) + 2;
    card2 = (rand()%13) + 2;

    face();
    disphanddealer();
}

void Dealer::disphanddealer()
{

    if ((card1 < 11) && (card2 < 11))
        cout <<"X " << card2 << endl << endl;
    else if ((card1 < 11) && (card2 >= 11))
        cout << "X " << card2f << endl << endl;
    else if ((card1 >= 11) && (card2 < 11))
        cout << "X " << card2 << endl << endl;
    else if ((card1 >= 11) && (card2 >= 11))
        cout << "X " << card2f << endl << endl;

}


Player.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include "Player.h"
#include "Dealer.h"
using namespace std;

Player::Player()
{
}

void Player::deal()
{
    card1 = (rand()%13) + 2;
    card2 = (rand()%13) + 2;

    face();
    disphand();

}

void Player::disphand()
{
    if ((card1 < 11) && (card2 < 11))
        cout << card1 << " " << card2 << endl << endl;
    else if ((card1 < 11) && (card2 >= 11))
        cout << card1 << " " << card2f << endl << endl;
    else if ((card1 >= 11) && (card2 < 11))
        cout << card1f << " " << card2 << endl << endl;
    else if ((card1 >= 11) && (card2 >= 11))
        cout << card1f << " " << card2f << endl << endl;
}

void Player::face()
{
        if (card1 == 11)
        {
            card1f = 'J';
        }
    else if (card1 == 12)
        {
            card1f = 'Q';
        }
    else if (card1 == 13)
        {
            card1f = 'K';
        }
    else if (card1 == 14)
        {
            card1f = 'A';
        }
    else if (card2 == 11)
        {
            card2f = 'J';
        }
    else if (card2 == 12)
        {
            card2f = 'Q';
        }
    else if (card2 == 13)
        {
            card2f = 'K';
        }
    else if(card2 == 14)
        {
            card2f = 'A';
        }

}

void Player::hit()
{
    system("CLS");
    Dealer hit; //I am not 100% sure how to use friends yet.  Am I right in creating a new object for dealer and using that?
    hit.disphanddealer(); // I think I may be doing something wrong here
    disphand();

}
Topic archived. No new replies allowed.