*Code that needs to be fixed by the gentle Heisenberg* #include <iostream> #incl
ID: 3835514 • Letter: #
Question
*Code that needs to be fixed by the gentle Heisenberg*
#include <iostream>
#include<fstream>
#include<cmath>
#include<cstdlib>
using namespace std;
int main()
{
int ran[5];
int arr[10][5];
int win[10];
int players;
ofstream file2;
file2.open("information.txt");
file2 << "The winner numbers are the following: Good Luck!" << endl;
cout << "Please enter the number of players that participate in game:" << endl;
cin >> players;
for (int i = 0; i < players; i++)
{
cout << "Enter the 5 numbers for player " << i << endl;
for (int j = 0; j < 5; j++)
cin >> arr[i][j];
}
for (int j = 0; j < 5; j++)
{
cout << " here" << j;
ran[j] = (rand() % 40) + 1;
file2 << " " << ran[j];
}
file2 << endl;
for (int i = 0; i < players; i++)
{
cout << " hereii" << i;
int c = 0;
for (int j = 0; j < 5; j++)
{
cout << " hereiii" << i;
for (int k = 0; k < 5; k++)
{
cout << " here iv" << i;
if (ran[j] == arr[i][k])
c++;
}
}
win[i] = c;
}
for (int i = 0; i < players; i++)
file2 << "For player " << i << " the number of random numbers matches are " << win[i] << endl;
for (int i = 0; i < players; i++)
{
switch (win[i])
{
case 5: file2 << "Player " << i << "Family dinner for four" << endl;
break;
case 4: file2 << "Player " << i << "Dinner for two" << endl;
break;
case 3: file2 << "Player " << i << " Free bottle of wine or pitcher of beer" << endl;
break;
case 2: file2 << "Player " << i << " Free glass of wine or pint of beer" << endl;
break;
case 1:
case 0: file2 << "Player " << i << " NO PRIZE (Not a winner)" << endl;
break;
}
}
return 0;
}
//THANK YOU
Explanation / Answer
#include <iostream>
#include<fstream>
#include<cmath>
#include<cstdlib>
using namespace std;
string getPrize(int matches)
{
switch (matches)
{
case 5: return "Family dinner for four";
case 4: return "Dinner for two";
case 3: return " Free bottle of wine or pitcher of beer";
case 2: return " Free glass of wine or pint of beer";
case 1:
case 0: return " NO PRIZE (Not a winner)";
}
}
void writeToFile(ofstream &out, string names[], int arr[][5], int win[], int n)
{
for(int i = 0; i < n; i++)
{
out << names[i] << " has entered ";
for(int j = 0; j < 5; j++)
{
out << arr[i][j] << " ";
}
out << " of which " << win[i] << " numbers matches. ";
out << "Prize won: " << getPrize(win[i]) << endl;
}
}
int main()
{
int ran[5];
int arr[200][5]; // your code was working for 10 players only, i made it for 200 as per question
int win[200];
string names[200];
int players;
// to write info to file
ofstream file2;
file2.open("information.txt");
file2 << "The winner numbers are the following: Good Luck!" << endl;
// random winning lottery. Its a good practice ofgenerating your results before user choose there number
// so that program seems fair
// Also your file needed it here as per above line.
for (int j = 0; j < 5; j++)
{
ran[j] = (rand() % 40) + 1;
file2 << " " << ran[j];
}
file2 << endl;
cout << "Please enter the number of players that participate in game:" << endl;
cin >> players;
string name;
for (int i = 0; i < players; i++)
{
cout << "Enter player name: ";
cin >> names[i];
cout << "Enter the 5 numbers for player " << i << endl;
for (int j = 0; j < 5; j++)
cin >> arr[i][j];
}
for (int i = 0; i < players; i++)
{
int c = 0;
for (int j = 0; j < 5; j++)
{
for (int k = 0; k < 5; k++)
{
if (ran[j] == arr[i][k])
{
c++;
break;
}
}
}
win[i] = c;
}
writeToFile(file2, names, arr, win, players);
file2.close();
return 0;
}
//THANK YOU
This sis how file content will look like:
The winner numbers are the following: Good Luck!
24 7 18 36 34
Maria has entered 1 80 54 23 2 of which 0 numbers matches. Prize won: NO PRIZE (Not a winner)
Daniel has entered 2 6 7 15 62 of which 1 numbers matches. Prize won: NO PRIZE (Not a winner)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.