Need HELP with arrays and structures

This is a discussion on Need HELP with arrays and structures within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, This is my first time here. I'm learning C++ and I'm having trouble with a problem. The question ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    3

    Unhappy Need HELP with arrays and structures

    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;

    }
    //***************************************

  2. #2
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    I dont fully understand the question, post the whole question and I'll be able to help better. Here is a couple of points though:

    -Always use code tags when posting code!!!!!!

    -int anarray[21]; //20 spaces of info
    Wrong. That is 21 spacesfrom anarray[ 0 ] to anarray[ 20 ]. Count 'em.

    -for(int index=1; index<=20; index++) .
    This loop doesn't start at the first record, it should be this (note the code tags)
    Code:
    for( int index = 0; index <= 20; index++ )

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    3

    Smile

    Thanks for replying Endo. I see what you mean about the index. I'm working on it, but I feel that isn't my problem. I tried to make four seperate arrays, but that isn't working either. Sorry about my coding.
    Here's the whole question.

    The local baseball team is computerizing its records. Write a program that computes batting averages. There are 20 players on the team, identified by the numbers 1 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 had 2 hits, 1 walk, 1 out. For Each player there are several lines in teh file. Each player's batting average is computed by adding the player's total number of hits and dividing by the hits + total outs. The program prints a table showing each player's identification number, batting average, and number of walks.


    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <string>
    
    using namespace std;
    
    void OpenForInput(ifstream&);  //function to prompt for file name		
    
    int main () 
    {
    	float sum1=0;			//adds hits
    	float sum2=0;			//adds walks
    	float sum3=0;			//adds outs
    	float hits;				//hits
    	float walks;			//walks
    	float outs;				//outs
    	float number;			//player number
    	ifstream data;			//reading data from file
    	
    OpenForInput(data);			//function to prompt for file name
    if(!data)
    return 1;
    
      int anarray[21];
      int aamer[21];
      int ummer[21];
      int raffat[21];
      
      for(int index=1; index<=20; index++)	
      {
    	  data >> number;
    
    	  anarray[index]=number;
     for(int index=1; index<=20; index++)	
      {
    	 data>> hits;
    	 aamer[index]=hits;
    	  for(int index=1; index<=20; index++)	
      {
    		  data>>walks;
    		  ummer[index]=walks;
    		   for(int index=1; index<=20; index++)	
      {	
    			   data>> outs;
    			   raffat[index]=outs;
    
    	  
     
    
     										//the computation to find
    										// batting average.
    float trial=0; 
    trial=trial+hits+outs;
    float roo=hits;
    float please=roo/trial;
    
     sum1=sum1+hits;
     sum2=sum2+walks;
     sum3=sum3+outs;
    
     //the output for each player
    cout<< "Player # "<<setw(2)<<number << "    "<<"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;
    
    }
    //***************************************

  4. #4
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    Not sure what all your requirements are, but the easiest method (without getting into any advanced data structures) would be to use one array of structs.

    Like so:
    Code:
    // create the struct
    struct PlayerStats
    {
        int PlayerID;
        int Hits;
        int Walks;
        int Outs;
    };
    
    // create an array
    PlayerStats StatsArray[20];
    
    // example to show how to reference the data in the struct
    for (int i = 0; i < 20; i++)
    {
        StatsArray[i].PlayerID = 0;
        StatsArray[i].Hits = 0;
        StatsArray[i].Walks = 0;
        StatsArray[i].Outs = 0;
    }
    Hope that helps -- at least to get you started.

    David
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

  5. #5
    Unregistered
    Guest
    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <string>
    
    using namespace std;
    
    //change return type to bool(ean)
    bool OpenForInput(ifstream&);  //function to prompt for file name		
    
    int main () 
    {
    	float hits;				//hits
    	float walks;			//walks
    	float outs;			//outs
    	float number;			//player number
    
    	ifstream data;			//reading data from file
      
      //if can't open file close program	
      if(!OpenForInput(data))
        return 0;
    
      int anarray[21]; //batting average array
      int aamer[21]; //hit array
      int ummer[21]; //walk array
      int raffat[21];  //outs array
    
      int index;
    
      //initialize arrays to all zeros
      for(index = 1; index <= 20; index++)
      {
          anarray[index] = aamer[index] = ummer[index] = raffat[index] = 0;
      }
      
      //as long as there is data in file to be read
      while(data)
      {
         data >> number;//number _is_  the reference index
         data >> hits;
         data >> walks;
         data >> outs;
         
         //add data read in to the appropriate spot
         aamer[number] += hits;
         umer[number] += walks;
         raffat[number] += outs;
      }
    
      //the computations to find batting average
      //could use number instead of index for all loops
      for(int index=1; index<=20; index++)	
      {
         float trial=0; //each player starts with no at bats;
         //add up results of all at bats for one player at a time
         trial = aamer[index] + umer[index] + raffat[index];
         //make sure that this player has been at bat
         if(trial != 0)
         {
           //do the actual calculation
           anarray[index] = aamer[index]/trial;//hits over at bats
         }
       }
    
       //the output for each player
       for(index = 1; index <= 20; index++)
       {
          cout<< "Player # "<<setw(2) << index << "    " << "Batting  
          avg: " << fixed << setprecision(3) << anarray[index] << "   
          Walks: " << setprecision(0) << umer[index] << endl;
       }
    
      return 0;
    }
    
    //***************************************
    bool 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;
        return false;
      }
      return true;
    }

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    3

    Smile

    Thank you so much for your e-mail David. I really appreciate it.

    I hope I can end up getting this to work.

    Regards,
    Mubbeena

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with arrays structures sorting.
    By pitifulworm in forum C Programming
    Replies: 42
    Last Post: 02-09-2009, 11:31 AM
  2. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  3. errors with arrays and structures
    By ssjnamek in forum C++ Programming
    Replies: 4
    Last Post: 03-03-2002, 10:48 PM
  4. Newbie Help (Arrays, Structures, Functions,Pointers)
    By tegwin in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2002, 05:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21