I have been stuck on this one problem for the last few days and this is my last resort. Any input is appreciated! Thanks in advance.
The requirements are to create a program that generates three random numbers and allows the user to guess three numbers, giving rewards for different matches. Currently, the program only recognizes one match and will not display if there is more than one number matching.
#include <iostream>
#include <ctime> // Needed for actual random number
#include <cstdlib>
using namespace std;
int main()
{
int guess1, guess2, guess3;
int random1, random2, random3;
int exactCount = 0;
int inexactCount = 0;
/// initialize random seed ///
srand(time(NULL));
/// Generate three random numbers ///
random1 = rand() % 9 + 1;
random2 = rand() % 9 + 1;
random3 = rand() % 9 + 1;
/// Accept three guesses ///
cout << "Guess a number between 1 and 9: ";
cin >> guess1;
cout << "Guess another number between 1 and 9: ";
cin >> guess2;
cout << "Guess another number between 1 and 9: ";
cin >> guess3;
/// Test random numbers ///
cout << "Number 1: " << random1 << endl << "Number 2: " << random2 << endl << "Number 3: " << random3 << endl;
system("PAUSE");
/// Count number of exact and inexact matches ///
if (guess1 == random1){
exactCount++;
}
else if (guess1 == random2 || guess1 == random3){
inexactCount;
}
if (guess2 == random2)
{
exactCount++;
}
else if (guess2 == random1 || guess2 == random3){
inexactCount;
}
if (guess3 == random3){
exactCount++;
}
else if(guess3 == random1 || guess3 == random2){
inexactCount++;
}
int matches = exactCount + inexactCount;
/// Check counts and display winnings ///
if (exactCount == 3){
cout << "JACKPOT!!! Three matching in a row! You win $100!!!";
}
else
{
if (matches == 0) cout << "No matches at all, you lose!" << endl;
else if (matches == 3) cout << "Three matching not in order! You win $50!" << endl;
else if (matches == 2) cout << "Two matching numbers! You win $20!" << endl;
else if (matches == 1) cout << "One matching number! You win $10!" << endl;
}
system("pause");
}
Aucun commentaire:
Enregistrer un commentaire