I think the single most important thing you can do is comment your code. It's hard to know what you're trying to do without them. It doesn't take much, just a sentence or two at the beginning to give an overview of the program and a single sentence or phrase above each function to say what it does.
CheckPrimary, CheckBonus, FillPrimary and FillBonus return a vector that isn't used. They should return void.
Check() is a vague name, and slightly misleading because it does more than just check the vector, it actually changes it. I'd call it RemoveDuplicates() instead because that's more description of what it does.
1 2 3 4 5
|
int main()
{
PrintNums();
return 0;
}
|
Don't do this without a good reason. Put the contents of PrintNum in main instead.
PrimaryNums duplicates much of the logic in FillPrimary.
PrimaryNums and BonusNums are nearly identical. You could reduce these to one function by passing in the desired length of the array. Same comment for CheckPrimary and CheckBonus
BonusNums prints two newlines before the header. Even though you may need those lines, this is the wrong place to print them. They are there because you know the call to BonusNums comes after PrimaryNums, but there's no reason that BonusNums should know or care where it's called. Instead, print the lines in main, after calling PrimaryNums and before calling BonusNums
It doesn't really matter in a small program like this, but it's usually a good idea to separate I/O from computation. In this case, it would be better to have the Fill functions fill the array, but not print it. That makes the functions much more flexible in case you decide to change the program later on.
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
|
// Generate and print 5 primary and 2 bonus number for a lottery
#include <iostream>
#include <time.h>
#include <vector>
#include <algorithm>
using namespace std;
void PrimaryNums();
void BonusNums();
void RemoveDuplicates(vector <int> &pNums);
void Fill(vector <int> &pNums, unsigned sz);
void Print(vector<int> &nums);
int main()
{
srand(time(0));
PrimaryNums();
cout << "\n\n";
BonusNums();
cout <<"\n";
return 0;
}
// Print the vector
void Print(vector<int> &nums)
{
for( auto a : nums)
cout << a << " ";
cout << endl;
}
// Generate and print 5 primary numbers
void PrimaryNums()
{
vector <int> pnums;
Fill(pnums, 5);
cout << "Your lucky primary numbers are: ";
Print(pnums);
}
// Generate and print 2 bonus numbers
void BonusNums()
{
vector <int> bnums = {};
Fill(bnums, 2);
cout << "Your lucky bonus numbers are: ";
Print(bnums);
}
// Remove duplicates from pNums
void RemoveDuplicates(vector <int> &pNums)
{
auto end = pNums.end();
for (auto it = pNums.begin(); it != end; ++it) {
end = std::remove(it + 1, end, *it);
}
pNums.erase(end, pNums.end());
}
// Fill pNums with sz unique numbers
void Fill(vector <int> &pNums, unsigned sz)
{
do {
while(pNums.size() < sz)
{
int random = rand () % 50 + 1;
pNums.push_back(random);
}
RemoveDuplicates(pNums);
} while (pNums.size() < sz);
}
|