So working on a project where we write to a file to track card game scores. At the start of the program, the user should be asked how many players there will be and then to enter their names. Based on my knowledge of C++ I thought an array to hold the names would be best. I'm open to other suggestions but here's what I've got. When I try to run the program it will ask for their names and then throw an exception "Write Access Violation". The best I could come up with is that I'm using auto and pointers wrong, but I'm not sure how I'd fix it.
#include <iostream>
#include <string>
using namespace std;
int getPlayerNum() {
cout << "How many people are playing?";
int length{};
cin >> length;
return length;
}
int getRounds(int number) {
cout << "How many rounds will you play?";
int number;
cin >> number;
return number;
}
void getPlayerName(string* names, int length) {
for (int i=1; i<= length; i++) {
cout << "Hello player " << i << ", What should I call you?";
cin << names[i];
}
}
int main ()
{
fstream gameFile;
int x, i, gameRound, playerScore;
int numPlayers{getPlayerNum()};
cout << "Welcome, I'll keep score while you play!";
getRounds(gameRound);
auto* playerNames{new string[numPlayers]{}}; //array to hold the names
getPlayerNames(playerNames, numPlayers);
gameFile.open("gameScore.txt", ios::out);
if (gameFile.is_open) {
for (x = 1; x <= gameRound; x++) {
gameFile << "\n==== Round #" << x << " ====" << endl;
for (i=1; i <= numPlayers; i++) {
cout << "Hello " << playerNames[i] << " , What was your score this round?";
cin >> playerScore;
gameFile << playerNames[i] << ": " << playerScore << endl;
}
}
gameFile.close()
}
}