I can't fathom this. It should probably be a dynamic array.

The following program needs a user set number of players. I then must calculate their batting average. The output will include playername, jersey number, average, and it must print on a clean screen for each player. I am not allowed to use global variables. I just don't know what to do or how to use dynamic arrays. They confuse me.



#include <iomanip>
#include <iostream>
#include <math.h>
using namespace std;
//Prototypes

void menu();
void cls();
void getinput();
void calculation(double bats[players], double hits[players], char playername[players] [30], double jersey[players]);
void output(double bats[players], double hits[players], char playername [players] [30], double average [players], double jersey[players]);


//main
int main()
{
cout << "Welcome to The Batting Program." << '\n';
cout << "Please choose from below: " << '\n';
menu();
}

void menu()
{
int choice;
cout << "1. Enter the information" << '\n';
cout << "2. Exit Batting Program" << '\n';
cout << "Enter the choice you would like below: ";
cin >> choice;

switch (choice)
{
case 1:
getinput();
break;

case 2:
return;
break;

default:
cout << '\n' << "Please choose from the available" << '\n';
menu();
break;
}

}
void getinput()
{
int players;
cout << "How many Players are on the Team? ";
cin >> players;

int counter;
char playername[players] [30];
double jersey[players];
double bats[players];
double hits[players];


for (counter = 0; counter < (players); counter ++)
{
cout << "What is the name of the player?";
cin>> playername [counter];
cin.ignore(1000, '\n');
cout << "What is that players jersey number";
cin >> jersey [counter];
cout << '\n';
cout << "How many times have you been at bat? ";
cin >> bats [counter];
cout << '\n';
cout << "How many times have you hit the ball?";
cin >> hits[counter];
}
calculation(bats, hits, playername, jersey);
}
void calculation(double bats[players], double hits[players], char playername[players] [30], double jersey[players])
{
int counter;
double average[players];
for (counter = 0; counter < (players); counter ++)
{
((average [counter]) = ((hits[counter])/(bats[counter])));
}
cls();
output(bats, hits, playername, jersey, average);
}
void cls()
{
system("cls");
}
void output(double bats[players], double hits[players], char playername [players] [30], double average [players], double jersey[players], int players)
{
int counter;
char junk [30];

cout << "Here is the information about the players: ";
for (counter = 0; counter < (players); counter ++)
{
cout << "Name: " << playername[counter];
cout << "Jersey Number: " << jersey[counter];
cout << "Batting Average: " << average[counter];
cout << "Press any key to continue ";
cin >> junk;
cls();
}
menu();
}
closed account (3TXyhbRD)
Use code tags. You're giving a hard time to read all that.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std; 
//Prototypes

void menu(); 
void cls();
void getinput();
void calculation(int, double [], double [], char *[30], double []);
void output(int , char *[30], double [], double []);


//main
int main()
{
  cout << "Welcome to The Batting Program." << '\n';
  cout << "Please choose from below: " << '\n';
  menu();
}

void menu()
{
  int choice; 
  cout << "1. Enter the information" << '\n'; 
  cout << "2. Exit Batting Program" << '\n';
  cout << "Enter the choice you would like below: "; 
  cin >> choice; 
  
  switch (choice)
  {
    case 1:
      getinput();
      break;
      
    case 2:
      return;
      break;
      
    default: 
      cout << '\n' << "Please choose from the available" << '\n';
      menu();
      break; 
  }
  
}

void getinput()
{
  int players; 
  cout << "How many Players are on the Team? ";
  cin >> players;
  cout <<endl;
  
  char* *playername = new char *[30];
  double* jersey = new double[players];
  double* bats = new double[players];
  double* hits = new double[players];
  
  
  for (int counter = 0; counter < players; ++counter)
  {
    cout << "What is the name of player " << counter + 1 << "? ";
    playername[counter] = new char[30];
    cin>> playername [counter];
    //cin.ignore(1000, '\n');
    
    cout << "What is " << playername[counter] << "'s jersey number? ";
    cin >> jersey [counter];
    
    cout << "How many times has " << playername[counter] <<" been at bat? ";
    cin >> bats [counter];
    
    cout << "How many times has " << playername[counter] << " hit the ball? ";
    cin >> hits[counter];
    cout << '\n';
  }
  calculation(players, bats, hits, playername, jersey);
}

void calculation(int players, double *bats, double *hits, 
		 char *playername[30], double* jersey)
{
  int counter;
  double* average = new double[players];
  
  for (counter = 0; counter < players; ++counter)
    ((average [counter]) = ((hits[counter])/(bats[counter])));
  
  cls();
  output(players, playername, jersey, average);
}

void cls()
{
  system("clear");
}

void output(int players, char *playername[30], double *jersey, double* average)
{
  int counter;
  char junk [30]; 
  
  cout << "Here is the information about the players:\n";
  for (counter = 0; counter < (players); counter ++)
  {
    cout << "Name: " << playername[counter] <<endl;
    cout << "Jersey Number: " << jersey[counter] <<endl;
    cout << "Batting Average: " << average[counter] * 100 <<"%" <<endl<<endl;
  }
  cout << "Press any key to continue..."; 
  cin >> junk;
  //cls(); 
  menu(); 
}



$ ./File
Welcome to The Batting Program.
Please choose from below: 
1. Enter the information
2. Exit Batting Program
Enter the choice you would like below: 1

How many Players are on the Team? 2
What is the name of player 1? Jerome
What is Jerome's jersey number? 34
How many times has Jerome been at bat? 27
How many times has Jerome hit the ball? 20

What is the name of player 2? David
What is David's jersey number? 2
How many times has David been at bat? 45
How many times has David hit the ball? 34

Here is the information about the players:
Name: Jerome
Jersey Number: 34
Batting Average: 74.0741%

Name: David
Jersey Number: 2
Batting Average: 75.5556%

Press any key to continue...d
1. Enter the information
2. Exit Batting Program
Enter the choice you would like below: 2



Your code is right on track except for the dynamic arrays.
Last edited on
Topic archived. No new replies allowed.