#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <stdio.h>
usingnamespace std;
void clrscr(){
#ifdef _WIN32 // Validate Windows OS
std::system("cls");
#else // Assume POSIX
std::system("clear");
#endif
}
int main(){
int rounds = 0; // Rounds initializer value
int hole = 0; // Hole initializer value
int players = 4; // Default maximum number of players per game
cout << "Welcome to the Golf Score Card program! " << endl; // Welcome message
cout << "How many rounds of golf would you like to play? (Enter 0 to exit) "; // Request to user for number of games to play before exiting program
cin >> rounds; cout << endl; // User inputs number of games they wish to enter information for
for(int i = 0; i <= rounds; i++){ // Golf gameplay loop limits number of rounds to number specified (exits program if number of rounds is zero)
cout << "How many players will be playing? (Max of 4) "; // Request to user for number of players in current iteration of game
cin >> players; cout << endl; // User inputs number of players
string *Player = new string [players]; // String array to hold Player names
int Scores [4][21]; // 18 Holes+Front and Back 9+Handicap+Total
// Used breakpoint here to find that I was missing an "=" sign in the next if statement
if(players == 1){ // If statement checks for single player game
cout << "Please enter your name. "; // Request to user to enter their name
cin >> Player[0]; cout << endl; // User enters their name
}
else{
cout << "Please enter the name of the first player. "; // Request to user to enter name of First player
cin >> Player[0]; cout << endl; // User enters name of first player in group into Player string array
for(int j = 1; j < players; j++){ // For loop to gather names of any remaining players
cout << "Please enter the name of the next player. "; // Request to user to enter name of any other players
cin >> Player[j]; cout << endl; // User enters names of remaining players of group into Player string array
}
}
clrscr(); // Clears screen of all previous outputs
cout << "Welcome "; // Custom welcome message to all players to the game
for(int k = 0; k < players; k++){ // for loop to list each player name in a grammatically correct style
cout << Player[k] << ", ";
}
cout << "to the game! " << endl << endl; // last section of custom welcome message
system("pause"); // Waits for user to read the Welcome message
cout << "Please enter the strokes for each player. " << endl << endl; // Request to user to enter strokes on each hole
for(int h = 0; h < 8; h++){ // Loop to display Score Card header for Front 9 holes (increments hole after all player scores are entered)
clrscr(); // Clears screen to prepare for Score Card output
cout << "Player " << setw(8) << "Hole1" << setw(6) << "Hole2" << setw(6) << "Hole3" << setw(6) << "Hole4"
<< setw(6) << "Hole5" << setw(6) << "Hole6" << setw(6) << "Hole7" << setw(6) << "Hole8" << setw(6) << "Hole9" << setw(6) << " Front9" << endl << endl;
for(int p = 0; p < players; p++){ // Stroke entry loop
cout << Player[p] << left << setw(8) << " "; // Outputs player name under player name column
int count = 0; // counter to display first through current holes played already
if (h > 0){ // if else statement to output scores already entered
while(count <= h){
cout << Scores[p][count] << left << setw(6);
cin >> Scores[p][h];
count++;
} // end while loop
} // end if
else{cin >> Scores[p][h]; count++;} // end else
count = 0; // reset counter to 0 to display scores from beginning again after screen clearing
}/* system("pause"); For Debugging*/
}
// Calculate First 9 for all players, output to array[9]
for(int p = 0; p < players; p++){
for(int h = 0; h < 8; h++){
Scores[p][9] += Scores[p][h];
}
}
for(int h = 9; h < 17; h++){ // Score Card table header for Back 9 holes (increments hole after all player scores are entered)
clrscr(); // Clears screen to prepare for Score Card output
cout << "Player " << setw(8) << "Hole10" << setw(7) << "Hole11" << setw(7) << "Hole12" << setw(7) << "Hole13"
<< setw(7) << "Hole14" << setw(7) << "Hole15" << setw(7) << "Hole16" << setw(7) << "Hole17" << setw(7) << "Hole18";
for(int p = 0; p < players; p++){ // Stroke entry loop
cout << Player[p] << left << setw(8) << " "; // Outputs player name under player name column
int count = 10; // counter to display tenth through current holes played already
if (h > 0){ // if else statement to output scores already entered
while(count <= h){
cout << setw(6) << Scores[p][count];
cin >> Scores[p][h];
count++;
} // end while loop
} // end if
else{cin >> Scores[p][h]; count++;} // end else
count = 0; // reset counter to 0 to display scores from beginning again after screen clearing
}/* system("pause"); For Debugging*/
clrscr(); // Clears screen to display updated Score Card
}
// Calculate Back 9 for all players, output to array[18]
for(int p = 0; p < players; p++){
for(int h = 10; h < 18; h++){
Scores[p][19] += Scores[p][h];
}
} system("pause"); // For Debugging
// Calculate Handicap for all players, output to array[19] *handicap = strokes - 72 (total among 18 holes)*
for(int p = 0; p < players; p++){
Scores[p][20] = (Scores[p][9] + Scores[p][19]) - 72;
}
// Calculate Total score for all players, output to array[21]
for(int p = 0; p < players; p++){ // Adds front 9 and back 9 holes and subtracts handicap from Total score
Scores[p][21] = (Scores[p][9] + Scores[p][19] - Scores[p][20]);
cout << "Player " << "Front 9 " << "Back 9 " << "Handicap " << "Total"; // Final table header for end game results
// Player incremented score output
cout << Player[p] << setw(8) << Scores[p][9] << setw(8) << Scores[p][19] << setw(7) << Scores[p][20] << setw(10) << Scores[p][21];
}
system("pause");
}
return 0;
}
When I run my program, it allows me to enter a score on each player, but then when I should be entering the second round of scores, the text "Hole 1" moves left one space and the program does not accept inputs. Additionally, the first round of scores are not displayed under the Hole 1 column.
A speedy resolution to this problem would be preferred as I am supposed to be presenting this program for my final tonight in about 12 hours. Yeah, I know, should have posted sooner, but I'm stubborn and wanted to solve the problem myself.
Just fixed the alignment of the table header and of first set of scores. However, I am still ending up with additional lines of number entries before next player's score can be entered.
Just fixed this by moving cin >> Scores[p][h]; from line 74 down to line 77 and cin >> Scores[p][h]; from line 101 down to line 104.
I also added an endl to cout << Player[p] << left << setw(8) << " "; on line 94 before Player[p] is output to the screen to bring the first players name below the Player column header.