Hi everyone,
This is my first time here. I'm learning C++ and I'm having trouble
with a problem. The question says there's a baseball team, identified by the numbers 1 through 20. Their records are coded as follows:
player's Identification #:
# of hits:
# of walks:
# of outs made in a game:
all in one line.
I don't understand how to write a code which checks if there
is more than one line for a player how do I sum the hits, walks, and outs, for that one player. Right now, I have one line of data for each player and it's working. I hope someone can help:
Here's my code:
// This program computes the batting average
// for a baseball team. There are 20 players
// on the team.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
void OpenForInput(ifstream&);
int main ()
{
float sum1=0;
float sum2=0;
float sum3=0;
float hits;
float walks;
float outs;
float number;
ifstream data;
OpenForInput(data);
if(!data)
return 1;
float BatterAvg();//*fill in);
int anarray[21]; //20 spaces of info
for(int index=1; index<=20; index++)
{
data >> number;
anarray[index]=number;
data>> hits >> walks >> outs;
float trial=0;
trial=trial+hits+outs;
float roo=hits;
float please=roo/trial;
sum1=sum1+hits;
sum2=sum2+walks;
sum3=sum3+outs;
cout<< "Player # "<<setw(2)<<index << " "<<"Batting avg: " << fixed<< setprecision(3)
<<please <<" Walks: "<<setprecision(0) <<sum2<< endl;
sum1=0; sum2=0; sum3=0;
trial=0; roo=0;
}
return 0;
}
//***************************************
void OpenForInput(ifstream& someFile)
{
string fileName;
cout <<"Input a file name." << endl;
cin >> fileName;
someFile.open(fileName.c_str());
if(!someFile)
cout <<"Can't open " << fileName << endl;
}
//***************************************