Hello. I\'ve been working on this program all day. It simulates a lottery. A use
ID: 3641880 • Letter: H
Question
Hello. I've been working on this program all day. It simulates a lottery. A user enters 5 integers, and the computer generates 5 random numbers, then it lets the user know if there are any matches (at least that's what it's supposed to do).*it keeps saying 'vector subscript out of range'. I'm not sure what that means. If you can find what's wrong and show me how to correct it, I will rate you Lifesaver :)
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
void getUserNumbers(vector <int> &U);
void getLotteryNumbers(vector <int> &L);
void compareNumbers (const vector<int> &U,const vector<int> &L);
int main()
{
char response = 'Y';
do {
vector <int> user;
getUserNumbers(user);
vector <int> lotterynums;
getLotteryNumbers(lotterynums);
const vector <int> compare1, compare2;
compareNumbers(compare1, compare2);
cout << "Would you like to play again (Y or N) ";
cin >> response;
} while(toupper(response) == 'Y');
}
void getUserNumbers(vector <int> &U)
{
int digits;
int userint;
const int SIZE = 5;
cout << "Please enter five digits in the range 0 to 9: " << endl;
for (int i=0;i<SIZE;i++)
{
do {
cin >> digits;
U.push_back (digits);
} while (digits < 0 || digits > 9);
}
}
void getLotteryNumbers(vector <int> &L)
{
int randnums;
const int SIZE = 5;
for(int i=0; i<SIZE; i++)
{
randnums = rand() % 9;
L.push_back (randnums);
}
}
void compareNumbers (const vector<int> &U,const vector<int> &L)
{
int count = 0 ;
const int Size = 5 ;
for (int i = 0; i < Size; i++)
{
for (int l = 0; l< Size; l++)
{
if (U[i] == L[i])
count++ ;
}
}
cout << "Your numbers: ";
for (int i=0; i <5; i++)
{
cout << U[i] << " ";
}
cout << "Official lottery drawing: ";
for (int i=0; i <5; i++)
{
cout << L[i] << " ";
}
cout << "You have successfully matched " << count << "numbers" << endl;
}
Explanation / Answer
// You had only one mistake that I corrected it. (The part that is in bold font.)
///////////
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
void getUserNumbers(vector <int> &U);
void getLotteryNumbers(vector <int> &L);
void compareNumbers (const vector<int> &U,const vector<int> &L);
int main()
{
char response = 'Y';
do {
vector <int> user;
getUserNumbers(user);
vector <int> lotterynums;
getLotteryNumbers(lotterynums);
compareNumbers(user, lotterynums); // -> This part
cout << "Would you like to play again (Y or N) ";
cin >> response;
} while(toupper(response) == 'Y');
}
void getUserNumbers(vector <int> &U)
{
int digits;
int userint;
const int SIZE = 5;
cout << "Please enter five digits in the range 0 to 9: " << endl;
for (int i=0;i<SIZE;i++)
{
do {
cin >> digits;
U.push_back (digits);
} while (digits < 0 || digits > 9);
}
}
void getLotteryNumbers(vector <int> &L)
{
int randnums;
const int SIZE = 5;
for(int i=0; i<SIZE; i++)
{
randnums = rand() % 9;
L.push_back (randnums);
}
}
void compareNumbers (const vector<int> &U,const vector<int> &L)
{
int count = 0 ;
const int Size = 5 ;
for (int i = 0; i < Size; i++)
{
for (int l = 0; l< Size; l++)
{
if (U[i] == L[i])
count++ ;
}
}
cout << "Your numbers: ";
for (int i=0; i <5; i++)
{
cout << U[i] << " ";
}
cout << endl ;
cout << "Official lottery drawing: ";
for (int i=0; i <5; i++)
{
cout << L[i] << " ";
}
cout << endl ;
cout << "You have successfully matched " << count << "numbers" << endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.