Class bool equals()

Hello everyone, I can't seem to figure out how to see if two of my cards are equal to each other in the lab I'm writing. Here is my code that I have so far.
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/***********************
CSC100- Lab 5
Programmer:Zachary Groom
***********************/
#include <iostream>
#include <string>
#include <sstream>		//string output stream

using namespace std;

// Card class
class CardType
{
	private:
		int rank;  //1-13
		char suit; // D,H,S,C
	public:
		CardType();
		void setCard ( int r, char s);
		void display ();
		int getRank ();
		char getSuit ();
		bool equals();
		int compareTo();
		string toString();
};

/**************************
 CardType implementation
**************************/
//default constructor -- sets the suit to S and rank to 1
CardType::CardType()
{
	cout<< "Default Set"<<endl;
	rank=1;
	suit='S';
}

// setCard will store the card in the object area
void CardType::setCard(int r, char s)
{	cout<<"Running setCard"<<endl;
	if (r >=0 && r<14)
		rank=r;
	else
		{
		cout<<"Invalid input setting rank to Ace."<<endl;
		rank=1;
		}
	if (s == 'D' || s=='d')
		{suit='D';}
	else if (s == 'H' || s=='h')
		{suit='H';}
	else if(s == 'S' || s=='s')
		{suit='S';}
	else if(s == 'C' || s=='c')
		{suit='C';}
	else
		{
		cout<<"Invalid input setting suit to Spade."<<endl;
		suit='S';
		}

}
int CardType::getRank()
{
	cout<<"Running getRank"<<endl;
	return rank;
}
char CardType::getSuit()
{
	cout<<"Running getSuit"<<endl;
	return suit;
}

/**************************
	Display
**************************/
void CardType::display()
{
	cout<<"The rank is: "<< rank<<"    The suit is: "<<suit<<endl;
}

/*************************
	equals
*************************/
bool equals()
{
	if (setcard() == setcard())
		{
			cout<<"returned true"<<endl;
			return true;
		}
	else
		{
			cout<<"returned false"<<endl;
			return false;
		}
}
/************************
Tostring returns the card
************************/
string CardType::toString()
{
	ostringstream outstring;
	if(rank==1)
		{rank='A';}
	else if (rank == 11)
		{rank='J';}
	else if (rank == 12)
		{rank='Q';}
	else if (rank == 13)
		{rank='K';}

	outString <<"The rank is: "<< rank<<"    The suit is: "<<suit<<endl;
	return outString.str();
}

/**************************
	Driver
**************************/
void main()
{
	CardType c;
	c.display();
	c.getRank();
	c.getSuit();
	c.setCard(5,'D');
	c.display();
	cout<< c.toString() <<endl;

	CardType a;
	a.display();
	a.getRank();
	a.getSuit();
	a.setCard(6,'D');
	a.display();

	//c.equals();

	system("pause");
}

and here is my assignment http://pastebin.com/eGWYcVcS

Im unsure as to how I would go about getting the cards to transfer to the bool equals() method.
Why make a new thread? Anyway, your professor explicitly tells you in the assignment that equals takes another Card as a parameter. Just compare this->rank with the parameters.rank, and this->suit with the parameters.suit. If both match, the cards are equal.
I hit the solved button and didn't know if people could still respond sorry.
If you were still wondering about your question in the other thread about(c : Card), the "c" is just a suggestion of what you should name the parameter, and "Card" is the type of the parameter, just like with setCard.
So
1
2
3
4
5
6
7
8
9
10
11
12
13
bool equals()
{
	if (rank == a.setcard(int) )
		{
			cout<<"returned true"<<endl;
			return true;
		}
	else
		{
			cout<<"returned false"<<endl;
			return false;
		}
}

I know its not right. I just don't know what I need to type in order to compare them. I think I'm comparing my c card to my a card but I'm not sure.
Ok, you need to pass a second card into equals as a parameter. The calling object gets passed in automatically, but you need another card to compare it to.
1
2
3
4
5
6
7
bool equals (Card c);
{
    if (rank == c.rank && suit == c.suit)
        return true;
    else
        return false;
}


That's all you need.
1
2
3
4
5
6
7
8
bool equals (CardType a);
{	
	cout<<"running equals"<<endl;
    if (rank == a.rank && suit == a.suit)
        {return true;}
    else
        {return false;}
}

It keeps saying the declaration terminated incorrectly.
I figured that one out
1
2
3
4
5
6
7
8
bool CardType::equals()
{
	cout<<"running equals"<<endl;
    if (rank == a.rank && suit == a.suit)
        {return true;}
    else
        {return false;}
}
and set it to this but now it says undefined symbol a in CardType::equals()
Last edited on
Topic archived. No new replies allowed.