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?
#include <iostream>
#include <vector>
#include <cstdlib>
usingnamespace 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;
}
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;
}