if statement - Three number lotttery guessing game C++ -
i have been stuck on 1 problem last few days , last resort. input appreciated! in advance.
the requirements create program generates 3 random numbers , allows user guess 3 numbers, giving rewards different matches. currently, program recognizes 1 match , not display if there more 1 number matching.
#include <iostream> #include <ctime> // needed 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 3 random numbers /// random1 = rand() % 9 + 1; random2 = rand() % 9 + 1; random3 = rand() % 9 + 1; /// accept 3 guesses /// cout << "guess number between 1 , 9: "; cin >> guess1; cout << "guess number between 1 , 9: "; cin >> guess2; cout << "guess number between 1 , 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 , 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 , display winnings /// if (exactcount == 3){ cout << "jackpot!!! 3 matching in row! win $100!!!"; } else { if (matches == 0) cout << "no matches @ all, lose!" << endl; else if (matches == 3) cout << "three matching not in order! win $50!" << endl; else if (matches == 2) cout << "two matching numbers! win $20!" << endl; else if (matches == 1) cout << "one matching number! win $10!" << endl; } system("pause"); }
this code should work fine! added in unordered_set
in case wanted increase cardinality of random numbers :)
#include <iostream> using std::cout; using std::cin; using std::endl; #include <unordered_set> using std::unordered_set; #include <cstdlib> #include <time.h> #include <array> using std::array; int main() { srand(time(null)); unordered_set<int> set_of_random_values; // insert 3 random values set of integers (int = 0; < 3; ++i) { set_of_random_values.insert(rand() % 9 + 1); } // ask user input array<int, 3> guesses; (auto& guess : guesses) { cout << "enter guess "; cin >> guess; } // print 3 values cout << "the 3 values : "; (auto integer : set_of_random_values) { cout << integer << ' '; } cout << endl; int matches = 0; (auto integer : guesses) { if (set_of_random_values.find(integer) != set_of_random_values.end()) { ++matches; } } if (matches == 0) cout << "no matches @ all, lose!" << endl; else if (matches == 3) cout << "three matching not in order! win $50!" << endl; else if (matches == 2) cout << "two matching numbers! win $20!" << endl; else if (matches == 1) cout << "one matching number! win $10!" << endl; return 0; }
and pointed out @paulmackinzie wanted should use loops have in above example , try not duplicate code well!
Comments
Post a Comment