Need help with homework

I've done most of the program myself but still having troubles on some parts of the if statements.In the program i'm suppose to put any three numbers that ranges 1-13 representing card numbers and letters. The three cards are suppose to look for in the if statements if the numbers a three pair, a pair, a straight, and just a high number or letter by itself. So far i've only done the three pair, but once I put different if statement it goes weird. I'd be much appreciated.

This is my code 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
  #include<iostream>
using namespace std;

const int ACE_CARD=1;
const int TWO_CARD=2;
const int THREE_CARD=3;
const int FOUR_CARD=4;
const int FIVE_CARD=5;
const int SIX_CARD=6;
const int SEVEN_CARD=7;
const int EIGHT_CARD=8;
const int NINE_CARD=9;
const int TEN_CARD=10;
const int JACK_CARD=11;
const int QUEEN_CARD=12;
const int KING_CARD=13;

int main(void)
{

	// 1. Input
	int card1, card2, card3;
	cout << "Enter three cards in the range ";
	cout << ACE_CARD << " to " << KING_CARD << ", " << "separated by spaces: ";
	cin >> card1 >> card2 >> card3;
	if (!cin)
	{
		cin.clear();
		cin.ignore(99,'\n');
		cout << endl << "Improper numeric format. Press ENTER to terminate program...";
		cin.ignore(99,'\n');
		return 1;
	}
	cin.ignore(999,'\n');
	if (card1 < ACE_CARD || card1 > KING_CARD
		|| card2 <ACE_CARD || card2 > KING_CARD
		|| card3 < ACE_CARD || card3 > KING_CARD)
	{
		cout << endl << "Input range error. Press ENTER to terminate:";
		cin.ignore(99,'\n');
		return 2;
	}
	// 2. Process
	if ( card1 && card2 >=ACE_CARD
		||card3 <=KING_CARD)
		cout << "You have a pair of aces." << endl;
	else
	
	if	(card1 >= KING_CARD && card2 && card3 <= KING_CARD)
		cout << "You have three kings." << endl;
	
	else 
	if (card1 >= QUEEN_CARD && card2 && card3 <= QUEEN_CARD)
		cout << "You have three queens." << endl;
	
	else 
	if (card1 >= JACK_CARD && card2 && card3 <= KING_CARD)
		cout << "You have three jacks." << endl;
	
	else 
	if (card1 >= TEN_CARD && card2 && card3 <= KING_CARD)
		cout << "You have three tens." << endl;
	
	else 
	if (card1 >= NINE_CARD && card2 && card3 <= KING_CARD)
		cout << "You have three nines." << endl;
	
	else 
	if (card1 >= EIGHT_CARD && card2 && card3 <= KING_CARD)
		cout << "You have three eights." << endl;
	
	else 
	if (card1 >= SEVEN_CARD && card2 && card3 <= KING_CARD)
		cout << "You have three sevens." << endl;
	
	else 
	if (card1 >= SIX_CARD && card2 && card3 <= KING_CARD)
		cout << "You have three sixes." << endl;
	
	else 
	if (card1 >= FIVE_CARD && card2 && card3 <= KING_CARD)
		cout << "You have three fives." << endl;
	
	else 
	if (card1 >= FOUR_CARD && card2 && card3 <= KING_CARD)
		cout << "You have three fours." << endl;
	
	else 
	if (card1 >= THREE_CARD && card2 && card3 <= KING_CARD)
		cout << "You have three threes." << endl;
	else
	if (card1 >= TWO_CARD && card2 && card3 <= KING_CARD)
		cout << "You have three twos." << endl;
	else
	if (card1 >= ACE_CARD && card2 && card3 <= ACE_CARD)
		cout << "You have three aces." << endl;

	// Finish up
	cout << "Press ENTER to Finish..." << endl;
	cin.ignore(999,'\n');
	return 0;
}
i dont see any problem in your code when i run it
This is a lot of code to figure out if you have three of a kind. My suggestion would be to sort the cards first, so you can start from a point of knowing which one is the high card, the middle card, and the low card. And write a function that will convert card numbers to names to save yourself a lot of code.
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
#include <iostream>
#include <algorithm> //for std::swap (C++98), use <utility> for C++11
#include <string>

using namespace std;

const int ACE_CARD=1;
const int TWO_CARD=2;
const int THREE_CARD=3;
const int FOUR_CARD=4;
const int FIVE_CARD=5;
const int SIX_CARD=6;
const int SEVEN_CARD=7;
const int EIGHT_CARD=8;
const int NINE_CARD=9;
const int TEN_CARD=10;
const int JACK_CARD=11;
const int QUEEN_CARD=12;
const int KING_CARD=13;

//pass in ints in any order and they'll come back sorted.
//Note pass by reference. The variables you pass in will be altered.
void sortThree(int& hi, int& mid, int& lo)
{
    if(hi < mid) std::swap(hi, mid);
    if(mid < lo) std::swap(mid, lo);
    if(hi < mid) std::swap(hi, mid);
}

std::string getCardName(int card)
{
    switch(card)
    {
    case ACE_CARD: return "ace";
    case TWO_CARD: return "two";
    case THREE_CARD: return "three";
    case FOUR_CARD: return "four";
    case FIVE_CARD: return "five";
    case SIX_CARD: return "six";
    case SEVEN_CARD: return "seven";
    case EIGHT_CARD: return "eight";
    case NINE_CARD: return "nine";
    case TEN_CARD: return "ten";
    case JACK_CARD: return "jack";
    case QUEEN_CARD: return "queen";
    case KING_CARD: return "king";
    default: return "";
    }
}

int main(void)
{
    // 1. Input
    int card1, card2, card3;
    cout << "Enter three cards in the range ";
    cout << ACE_CARD << " to " << KING_CARD << ", " << "separated by spaces: ";
    cin >> card1 >> card2 >> card3;
    if (!cin)
    {
        cin.clear();
        cin.ignore(99,'\n');
        cout << endl << "Improper numeric format. Press ENTER to terminate program...";
        cin.ignore(99,'\n');
        return 1;
    }
    cin.ignore(999,'\n');
    if (card1 < ACE_CARD || card1 > KING_CARD
        || card2 <ACE_CARD || card2 > KING_CARD
        || card3 < ACE_CARD || card3 > KING_CARD)
    {
        cout << endl << "Input range error. Press ENTER to terminate:";
        cin.ignore(99,'\n');
        return 2;
    }

    //make card1 the high card, card3 the low card
    sortThree(card1, card2, card3);

    if(card1 == card2 && card2 == card3)
    {
        std::cout << "You have three " << getCardName(card1) << "s.\n";
    }
}
There is actually a glaring problem in the code. It has to do with how you are checking for a three of a kind.

For example if we just take one of the if statements and break it down.

if (card1 >= ACE_CARD && card2 && card3 <= ACE_CARD)

What this statement is saying is this

1.) card1 >= 1 ~ If card1 is greater then or equal to 1 return true and continue to 2.)

2.) card2 ~ Basically this is always true no matter what number it is and continues to 3.)

3.) card3 <= 1 ~ If card3 is less then or equal to 1 the if statement finally returns true since all conditions are met.

Now what happens if we entered a input something like this 9 1 1? It will print that I have three aces when I really don't.

http://ideone.com/TrSHhh


Now how do we go about fixing this problem? There is multiple different ways to solve this problem, but the simplest one would be to just check each card with ==. For example your three aces if statement would look like this.

if (card1 == ACE_CARD && card2 == ACE_CARD && card3 == ACE_CARD)

As I said there is different solutions for this but that is the simplest though not really the best. As for you other problem could you elaborate what you mean by "it goes wierd"? Be as detailed as you can about the results you are getting.

Last edited on
When I put another different if statement let's say for a pair for ACE_CARD. The pair always overtakes the three pairs whenever I input three ones. It will always say "You have two aces."
I fixed all of my problem for the three now I just need help on this one.
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
if ( card1 && card2 ==ACE_CARD
||card3 >=ACE_CARD)
cout << "You have a pair of aces." << endl;
else

if	(card1 == KING_CARD && card2 == KING_CARD && card3 == KING_CARD)
cout << "You have three kings." << endl;

else 
if (card1 == QUEEN_CARD && card2 == QUEEN_CARD && card3 == QUEEN_CARD)
cout << "You have three queens." << endl;

else 
if (card1 == JACK_CARD && card2 == JACK_CARD && card3 == JACK_CARD)
cout << "You have three jacks." << endl;

else 
if (card1 == TEN_CARD && card2 == TEN_CARD && card3 == TEN_CARD)
cout << "You have three tens." << endl;

else 
if (card1 == NINE_CARD && card2 == NINE_CARD && card3 == KING_CARD)
cout << "You have three nines." << endl;

else 
if (card1 == EIGHT_CARD && card2 == EIGHT_CARD && card3 == EIGHT_CARD)
cout << "You have three eights." << endl;

else 
if (card1 == SEVEN_CARD && card2 == SEVEN_CARD && card3 == SEVEN_CARD)
cout << "You have three sevens." << endl;

else 
if (card1 == SIX_CARD && card2 == SIX_CARD && card3 == SIX_CARD)
cout << "You have three sixes." << endl;

else 
if (card1 == FIVE_CARD && card2 == FIVE_CARD && card3 == FIVE_CARD)
cout << "You have three fives." << endl;

else 
if (card1 == FOUR_CARD && card2 == FOUR_CARD && card3 == FOUR_CARD)
cout << "You have three fours." << endl;

else 
if (card1 == THREE_CARD && card2 == THREE_CARD && card3 == THREE_CARD)
cout << "You have three threes." << endl;
else
if (card1 == TWO_CARD && card2 == TWO_CARD && card3 == TWO_CARD)
cout << "You have three twos." << endl;
else
if (card1 == ACE_CARD && card2 == ACE_CARD && card3 == ACE_CARD)
cout << "You have three aces." << endl
Remember that code runs from top to bottom. The same goes for your if statements. Whenever it finds a if statement that returns true it will skip over all the rest.

So you want to make sure to structure them in a way that makes sure that won't happen unless it is suppose to.

For example instead of checking for a pair first, instead do your three of a kind checks first and if they fail move onto the pair checks.
Thanks you for that, but now i'm having trouble with the pairs. If i put two of the same if statements with slightly updated to two. The first pair statements overtakes the 2nd pair if statement. If I input 2 2 1 it comes out "You have a pair of aces."
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
if (card1 == KING_CARD && card2 == KING_CARD && card3 == KING_CARD)
		cout << "You have three kings." << endl;
	
	else 
	if (card1 == QUEEN_CARD && card2 == QUEEN_CARD && card3 == QUEEN_CARD)
		cout << "You have three queens." << endl;
	
	else 
	if (card1 == JACK_CARD && card2 == JACK_CARD && card3 == JACK_CARD)
		cout << "You have three jacks." << endl;
	
	else 
	if (card1 == TEN_CARD && card2 == TEN_CARD && card3 == TEN_CARD)
		cout << "You have three tens." << endl;
	
	else 
	if (card1 == NINE_CARD && card2 == NINE_CARD && card3 == KING_CARD)
		cout << "You have three nines." << endl;
	
	else 
	if (card1 == EIGHT_CARD && card2 == EIGHT_CARD && card3 == EIGHT_CARD)
		cout << "You have three eights." << endl;
	
	else 
	if (card1 == SEVEN_CARD && card2 == SEVEN_CARD && card3 == SEVEN_CARD)
		cout << "You have three sevens." << endl;
	
	else 
	if (card1 == SIX_CARD && card2 == SIX_CARD && card3 == SIX_CARD)
		cout << "You have three sixes." << endl;
	
	else 
	if (card1 == FIVE_CARD && card2 == FIVE_CARD && card3 == FIVE_CARD)
		cout << "You have three fives." << endl;
	
	else 
	if (card1 == FOUR_CARD && card2 == FOUR_CARD && card3 == FOUR_CARD)
		cout << "You have three fours." << endl;
	
	else 
	if (card1 == THREE_CARD && card2 == THREE_CARD && card3 == THREE_CARD)
		cout << "You have three threes." << endl;
	else
	if (card1 == TWO_CARD && card2 == TWO_CARD && card3 == TWO_CARD)
		cout << "You have three twos." << endl;
	else
	if (card1 == ACE_CARD && card2 == ACE_CARD && card3 == ACE_CARD)
		cout << "You have three aces." << endl;
	else 
	if ( card1 && card2 ==ACE_CARD
		||card3 >=ACE_CARD)
		cout << "You have a pair of aces." << endl;
	else 
	if ( card1 && card2 == TWO_CARD
		||card3 >=ACE_CARD)
		cout << "You have a pair of twos." << endl;
SORT THE CARDS. If you sort them such that card1 is the highest, card2 is the middle, and card 3 is the smallest, then you can boil the logic down to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    //assuming card1 is the biggest, card2 is in the middle, and card3 is lowest
    if(card1 == card2 && card2 == card3)
    {
        std::cout << "You have three " << card1 << "s.\n";
    }
    else if (card1 == card2 || card2 == card3)
    {
        std::cout << "You have a pair of " << card2 << "s.\n";
    }
    else if ( (card1 == card2 + 1) && (card2 == card3 + 1) )
    {
        std::cout << "You have a straight with " << card1 << " high.\n";
    }
    else
    {
        std::cout << "Your highest card is " << card1 << ".\n";
    }

If you must print a card name instead of just a number, write a function that translates a number into a name. There is an example in my previous post in this topic.
I did the shorter way of doing the if statement, but i'm getting numbers instead of words. I did everything changing the numbers into words but still it comes out "You have three 1s."
This is what 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
#include<iostream>
using namespace std;

const int ACE_CARD=1;
const int TWO_CARD=2;
const int THREE_CARD=3;
const int FOUR_CARD=4;
const int FIVE_CARD=5;
const int SIX_CARD=6;
const int SEVEN_CARD=7;
const int EIGHT_CARD=8;
const int NINE_CARD=9;
const int TEN_CARD=10;
const int JACK_CARD=11;
const int QUEEN_CARD=12;
const int KING_CARD=13;

void sortThree(int& hi, int& mid, int& low)
{
	if(hi<mid) std:: swap(hi,mid);
	if(mid<low) std::swap(mid, low);
	if(hi<mid) std::swap(hi,mid);
}

 string getCardName(int card)
{
	switch(card)
	{
	case ACE_CARD: return "ace";
	case TWO_CARD: return "two";
	case THREE_CARD: return "three";
	case FOUR_CARD: return "four";
	case FIVE_CARD: return "five";
	case SIX_CARD: return "six";
	case SEVEN_CARD: return "seven";
	case EIGHT_CARD: return "eight";
	case NINE_CARD: return "nine";
	case TEN_CARD: return "ten";
	case JACK_CARD: return "jack";
	case QUEEN_CARD: return "queen";
	case KING_CARD: return "king";
	}
}

int main(void)
{

	// 1. Input
	int card1, card2, card3;
	cout << "Enter three cards in the range ";
	cout << ACE_CARD << " to " << KING_CARD << ", " << "separated by spaces: ";
	cin >> card1 >> card2 >> card3;
	if (!cin)
	{
		cin.clear();
		cin.ignore(99,'\n');
		cout << endl << "Improper numeric format. Press ENTER to terminate program...";
		cin.ignore(99,'\n');
		return 1;
	}
	cin.ignore(999,'\n');
	if (card1 < ACE_CARD || card1 > KING_CARD
		|| card2 <ACE_CARD || card2 > KING_CARD
		|| card3 < ACE_CARD || card3 > KING_CARD)
	{
		cout << endl << "Input range error. Press ENTER to terminate:";
		cin.ignore(99,'\n');
		return 2;
	}
	// 2. Procss
	sortThree(card1, card2, card3);

	if(card1 == card2 && card2 == card3)
		cout << "You have three " << card1 << "s." << endl;
	else if (card1 ==card2 || card2 ==card3)
		cout << "You have a pair of " << card2 << "s." << endl;
	else if ((card1 == card2 + 1) && (card2 == card3 + 1))
		cout << "You have a " << card1 << "-high straight." << endl;
	else
		cout << "You have a(n) " << card1 << "." << endl;



	// Finish up
	cout << "Press ENTER to Finish..." << endl;
	cin.ignore(999,'\n');
	return 0;

That's where the name conversion function comes in handy. Instead of saying this:
cout << "You have three " << card1 << "s." << endl;
you can write this:
cout << "You have three " << getCardName(card1) << "s." << endl;
You can apply this to the other output statements, too. Just make sure you pass the correct card into the getCardName function.
But here's the problem I don't get. If I put cout << "You have three" << getCardName(card1) << "s." << endl; I get a synntax error on << before getCardName(card1). It says that << has no operator
You need to #include <string> .
1
2
else if (card1 ==card2 || card2 ==card3)
		cout << "You have a pair of " << card2 << "s." << endl;


Not quite. what if card1 == card3?
If the cards are sorted, then that can only happen if there's three-of-a-kind, which is handled in the previous case.
Good point. guess I should read the whole post before spouting off. :)
I did the problems by doing the long way, but now i'm having trouble with single where one is always higher than the last two this is my code
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
  
if (((card1 == ACE_CARD) || card2 == ACE_CARD || card3 == ACE_CARD))
		cout << "You have an ace." << endl;
	else
	if (((card1 == TWO_CARD || card2 == TWO_CARD || card3 == TWO_CARD)))
		cout << "You have a two." << endl;
	else
	if (((card1 == THREE_CARD || (card2 == THREE_CARD) || card3 == THREE_CARD)))
		cout << "You have a three." << endl;
	else
	if (((card1 == FOUR_CARD || (card2 == FOUR_CARD) || card3 == FOUR_CARD)))
		cout << "You have a four." << endl;\
	else
	if (((card1 == FIVE_CARD || (card2 == FIVE_CARD) || card3 == FIVE_CARD)))
		cout << "You have a five." << endl;
	else
	if (((card1 == SIX_CARD || (card2 == SIX_CARD) || card3 == SIX_CARD)))
		cout << "You have a six." << endl;
	else
	if (((card1 == SEVEN_CARD || (card2 == SEVEN_CARD) || card3 == SEVEN_CARD)))
		cout << "You have a seven." << endl;
	else
	if (((card1 == EIGHT_CARD || (card2 == EIGHT_CARD) || card3 == EIGHT_CARD)))
		cout << "You have an eight." << endl;
	else
	if (((card1 == NINE_CARD || (card2 == NINE_CARD) || card3 == NINE_CARD)))
		cout << "You have a nine." << endl;
	else
	if (((card1 == TEN_CARD || (card2 == TEN_CARD) || card3 == TEN_CARD)))
		cout << "You have a ten." << endl;
	else
	if (((card1 == JACK_CARD || (card2 == JACK_CARD) || card3 == JACK_CARD)))
		cout << "You have a jack." << endl;
	else
	if (((card1 == QUEEN_CARD || (card2 == QUEEN_CARD) || card3 == QUEEN_CARD)))
		cout << "You have a queen." << endl;
	else
	if (((card1 == KING_CARD || (card2 == KING_CARD) || card3 == KING_CARD)))
		cout << "You have a king." << endl;
Topic archived. No new replies allowed.