dice game vectors

Feb 5, 2014 at 1:24am
I am finishing up a dice game that takes an inputted number from the user and outputs 5 dice rolled. I am stuck on the last part of a "pair". If I get two separate pairs it will output the message twice. I am trying to get it if there are two separate pairs then it will output "You got two pairs!". How do I do this?

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
#include <iostream>
#include <vector>
#include <cstdlib>

using namespace std;

void seedRNG();
int rollDie();
void rollAndReport();


int main()
{
    seedRNG();
    rollAndReport();

    system("PAUSE");
    return EXIT_SUCCESS;

}

void rollAndReport()
{
    vector<unsigned> frequency(7); 

    cout << "The rolled dice: ";
    for (unsigned i = 0; i < 5; ++i)
    {
        int roll = rollDie();
        cout << '[' << roll << "] ";
        ++frequency[roll];
    }

    cout << "\nThe frequencies: ";

    for (unsigned i = 1; i < frequency.size(); ++i){
    cout << i << " -> " << frequency[i] << "  ";
}
    cout << '\n';

    for (unsigned i = 1; i < frequency.size(); ++i)
    {
        if (frequency[i] == 5){
        cout<<"You have five of a kind!"<<endl;
        }
        if (frequency[i] == 4){
        cout<<"You have four of a kind!"<<endl;
        }
        if (frequency[i] == 3){
        cout<<"You have three of a kind!"<<endl;
        }
        if (frequency[i] == 2){
        cout<<"You have one pair!"<<endl;
        }
        
}

}

void seedRNG()
{
    int seed;
    cout << "Enter an integer between 1 and 10000: ";
    cin >> seed;
    srand(seed);
}


int rollDie()
{
    return (rand() % 6) + 1;
}
Feb 5, 2014 at 3:41am
You are going to want to count how many pairs you have first. Then if pairs is equal to 2 output "You have two pairs" otherwise output "you have one pair"

You could do this several ways. One method (probably not the best) would be like this:

1
2
3
4
5
6
int pairs = 0;
for( int i = 1; i < frequency.size(); ++i ) //weird how you do this instead of 0-5
{
    if( frequency[i] == 2 )
        ++pairs;
}


Feb 5, 2014 at 12:41pm
giblit yours makes sense for counting the pairs but where would I put the output "You have one pair!" in your loop or if statement?
Feb 5, 2014 at 4:12pm
After the loop. How can you report the number of pairs before you've finished counting them?
Topic archived. No new replies allowed.