|
-
Jun 23rd, 2002, 05:39 PM
#1
Thread Starter
Junior Member
please help.. but this is VC++
The local baseball team is computerizing its records. You are to write a program that computes batting averages. There are 20 players on the team, identified by the numbers I through 20. Their batting records are coded in a file as follows.
Each line contains four numbers; the player's identification number and the number of hits, walks, and outs he or she made in a particular game. Here is a sample;
3 2 1 1
The example above indicates that during a game, player number 3 was at bat four times and made 2 hits, I walk, and lout. For each player there are several lines in the file. Each player's batting average is computed by adding the player's total number of hits and dividing by the total number of times at bat. A walk
does not count as either a hit or a time at bat when the batting average is being calculated. Your program prints a table showing each player's identification number, batting average, and number of walks. (Be careful; The players' identification numbers are I through 20, but C++ array indexes start at 0.)
-
Jun 23rd, 2002, 06:05 PM
#2
Frenzied Member
First, this is C++, NOT Visual C++ (VC++)
Second, C++ questions go in the C++ forum - that's why it's there.
OK, try this:
Code:
#include <fstream.h>
#include <iomanip.h>
struct player
{
int hit;
int battime;
}
player Team[20];
int main()
{
ifstream input;
int number, hit, walk, out;
for(int i = 0; i < 20; i++)
{
Team[i].hit = 0;
Team[i].battime = 0;
}
input.open("batting.dat");
while (!input.eof())
{
input >> number >> hit >> walk >> out;
Team[number - 1].hit += hit;
Team[number - 1].battime += (hit + out);
// Note: these are for compatibility reasons
number = 1;
hit = 0;
walk = 0;
out = 0;
}
cout << "ID# Bat Avg Walks";
cout << "---------------------------";
for(i = 0; i < 20; i++)
cout << setw(9) << i + 1 << setw(9) << Team[i].hit << setw(9) << Team[i].battime;
return 0;
}
MicroBasic
Dragon Shadow Trainer
There is no good or evil in the world...only programmers and fools .
-
Jun 23rd, 2002, 06:27 PM
#3
Hyperactive Member
-
Jun 23rd, 2002, 09:15 PM
#4
Frenzied Member
In response to your PM:
Code:
#include <fstream.h>
#include <iomanip.h>
struct player
{
int hit;
int battime;
}
player Team[20];
int main()
{
ifstream input;
int number, hit, walk, out;
for(int i = 0; i < 20; i++)
{
Team[i].hit = 0;
Team[i].battime = 0;
}
input.open("batting.dat");
while (!input.eof())
{
input >> number >> hit >> walk >> out;
Team[number - 1].hit += hit;
Team[number - 1].battime += (hit + out);
// Note: these are for compatibility reasons
number = 1;
hit = 0;
walk = 0;
out = 0;
}
input.close(); //add this
cout << "ID# Bat Avg Walks";
cout << "---------------------------";
for(i = 0; i < 20; i++)
cout << setw(9) << i + 1 << setw(9) << Team[i].hit << setw(9) << Team[i].battime;
return 0;
}
If it didn't work, check if you added batting.dat in the same folder as the program with the data in THAT format.
Also, don't PM people to respond to their posts. Use Reply. PM is used for certain messages and notifications, not responses.
MicroBasic
Dragon Shadow Trainer
There is no good or evil in the world...only programmers and fools .
-
Jun 24th, 2002, 03:51 AM
#5
PowerPoster
You shouldn't do peoples work for them!!
-
Jun 24th, 2002, 09:11 AM
#6
Agree. He didnt even ask...
Z.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|