I am taking a C++ course and we have received our final assignment, I was looking for some suggestions and or tips so I can continue to improve.
I am aware that the use of using namespace std
is bad practice but my professor prefers that we use this way since it is a beginners course.
The assignment was a two part assignment I have commented out the piece of the code that was unneeded for the second part.
Part One:
Write a program that simulates a lottery. The program should have an array of 7 integers named winningDigits, with a randomly generated number in the range of 0 through 9 for each element in the array. The program should ask the user to enter 7 digits and should store them in a second integer array named player. The program must compare the corresponding elements in the two arrays and count how many digits match.
For example, the following shows the winningDigits array and the player array with sample numbers stored in each. There are two matching digits, elements 2 and 4.
WinningDigits: 7 4 9 1 3 5 0
Player: 4 2 9 7 3 4 2
Part Two:
Modify the lab solution so that the player numbers are entered not from the keyboard, but from the file player.dat (you will need to create this file and add 7 numbers).
What can I do better?
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
const int size = 7;
int winningDigits[size];
int player[size];
int count=0;
ifstream datafile;
datafile.open("player.dat");
for(int i=0; i<size; i++)
{
winningDigits[i]=rand()%10;
datafile >> player[i];
if (winningDigits[i]==player[i])
count++;
}
/*
for(int i=0; i<size; i++)
{
winningDigits[i]=rand()%10;
cout << "Enter number " << i+1 << " ";
cin >> player[i];
if (winningDigits[i]==player[i])
count++;
}
*/
cout << "WinningDigits: ";
for(int i=0; i<size; i++)
cout << winningDigits[i] << " ";
cout << "\nPlayer: ";
for(int i=0; i<size; i++)
cout << player[i] << " ";
cout << " \nThere are " << count << " matching elements ";
for(int i=0; i<size; i++)
if(winningDigits[i]==player[i])
cout << i+1 << " ";
cout << "\n";
}