std::vector<int> lottery, user;
lottery.push_back(45);
lottery.push_back(22);
lottery.push_back(11);
lottery.push_back(4);
lottery.push_back(9);
// Vectors can have numerous types
// So lets create a simple generic function
template<typename T>
bool Contains(const std::vector<T>& v1, T val)
{
// Loop through contents
for(size_t i = 0; i < v1.size(); i++)
if(v1[i] == val) returntrue; // if element contains value return true
returnfalse; // If execution survives loop the value does not exist in vector
}
while(user.size() < lottery.size()) // Makes it a little more scalable rather than magic numbers
{
std::cout << "Please enter a number: ";
int temp;
std::cin >> temp;
// Best to check input but omitted for clarity
if(Contains(user, temp)) // Check if number already used - because temp is int I believe compiler can resolve template ( Don't worry about that too much )
std::cout << "You already chose that number!\n\n";
else
user.push_back(temp); // Add it to user choices
// Keep repeating till user has picked 5 numbers
}
// sort the vectors from lowest to highest
// NOTE - Must include <algorithm>
std::sort(lottery.begin(), lottery.end());
std::sort(user.begin(), user.end());
// Check for equality
if(user == lottery) std::cout "You won!\n\n";
else std::cout << "You lost!\n\n";
EDIT: An extension to CodeWriters approach
1 2 3 4 5 6 7 8 9 10 11 12 13
bool Equal(int& array1, int& array2, size_t elements)
{
for(size_t i = 0; i < elements; i++) // loop through arrays
// if elements don't match they are not equal
if(array1[i] != array2[i]) returnfalse;
// If execution survives the loop the arrays are equal
returntrue;
}
int lottery[5], user[5];
if(Equal(lottery,user,5)) std::cout << "You won!\n\n";
else std::cout << "You lost!\n\n";
EDIT 3:
Fixed minor bug in vector version
EDIT 4:
Forgot to sort vectors ( Sorry it's half 2 in the morning :)