Thread: Help passing arrays through functions

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    68

    Help passing arrays through functions

    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <fstream>
    #include <cstdlib>
    
    using namespace std;
    
    void ReadPlayerInfo(ifstream& myIn, string NameArray[], int idArray[], int& size);
    void ReadProcessBatting (ifstream& myIn, const int id [], float batAvg [], int walkArray[],int size);
    void ProcessRecord(string batRec, float& batAvg, int& walk);
    int SearchPosition(const int idArray[], int id, int size);
    void PrintTable(string nameArray[],int idArray[],float averageArray[],int walkArray[],int size);
    int FindHigh(float averageArray[], int size);
    int FindLow(float averageArray[], int size);
    
    int main()
    {
    	string PlayerRecordsFile;
    	string PlayerBattingFile;
    	
    	ifstream myIn;
    	ifstream myIn2;
    	
    	cout << "Enter file name that contains player records: ";
    	cin >> PlayerRecordsFile;
    	myIn.open(PlayerRecordsFile.c_str());
    	
    	while (!myIn)
    	{
    		cout << "Enter file name that contains player records: ";
    		cin >> PlayerRecordsFile;
    		myIn.open(PlayerRecordsFile.c_str());
    	}
    		
    	cout << "Enter file name that contains batting data: ";
    	cin >> PlayerBattingFile;
    	myIn2.open(PlayerBattingFile.c_str());
    	
    	while (!myIn2)
    	{
    		cout << "Enter file name that contains batting data: ";
    		cin >> PlayerBattingFile;
    		myIn2.open(PlayerBattingFile.c_str());
    	}
    	
    	return 0;
    		
    }
    
    void ReadPlayerInfo(ifstream& myIn, string NameArray[], int idArray[], int& size)
    {
    	string playerName;
    	int playerNumber;
    	myIn >> playerName;
    	NameArray[0] = playerName;
    	myIn >> playerNumber;
    	idArray[0] = playerNumber;
    }
    This is what I have so far. Basically for the first data file which store player records. I need to read in their name and number and store it to two different arrays.

    the data file looks like

    name number
    name number
    ...
    name number

    What exactly does my function call inside main need to look like for it?
    I thought this but i was dead wrong i guess
    Code:
    ReadPlayerInfo(ifstream& myIn, string NameArray, int idArray, int& size);

  2. #2
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    you should be aware that doesn't pass the array. that passes a pointer to the beginning of the array.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    Quote Originally Posted by m37h0d View Post
    you should be aware that doesn't pass the array. that passes a pointer to the beginning of the array.
    Thank you I realize that. But still im not sure how to go about calling the function. And in that function definition. Say I have 2 files and they have a different number of players. How can I easily make the size of the array match. I realize I could read through and increment the counter then store the data. But can I make the array the exact size i need to while I read in the names and numbers

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    I liked this code example from cpjust >

    see this post >

    Whats the deal with pointers eh?

    But if you know how many entries there are in your file you can set the array to that size if its not too big and then just use the index variable to control how far to read into the array
    Last edited by rogster001; 11-19-2009 at 05:02 AM.

  5. #5
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    try:
    Code:
    struct PlayerInfo
    {
      std::string name;
      int id;
      float batAvg;
      int walk;
    };
    
    std::vector<PlayerInfo>players;

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    Hmmm I'm fairly new to structures and no clue on vectors. How exactly does that work? I would like to understand it thoroughly before I go about using it. Thank you for you help though. Much appreciated.

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    Ok so I understand vectors are like arrays.

    They handle the information automatically correct? So if I keep adding stuff they expand as needed?

    If I wanted to use an element in a vector what would that look like?

    Since the vectors store 4 different variables how do i access the specific one i want? for example I need to do stuff with name in one instance but in the next i need the ids

  8. #8
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    use the . operator to access members of structures.

    Code:
    struct PlayerInfo
    {
      std::string name;
      int id;
      float batAvg;
      int walk;
    };
    
    PlayerInfo pi;
    pi.name = "Babe Ruth";
    pi.id =0;
    pi.batAvg = .438;
    pi.walk = 10000;
    
    std::vector<PlayerInfo>players;
    players.push_back(pi);
    the vectors don't store 4 different variables, they store 1 kind of variable that has 4 members.

    you can use the [] syntax to access elements of vectors just like arrays.

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    Code:
    // vector assign
    #include <iostream>
    #include <vector>
    using namespace std;
    
    int main ()
    {
      vector<int> first;
      vector<int> second;
      vector<int> third;
    
      first.assign (7,100);             // a repetition 7 times of value 100
    
      vector<int>::iterator it;
      it=first.begin()+1;
    
      second.assign (it,first.end()-1); // the 5 central values of first
    
      int myints[] = {1776,7,4};
      third.assign (myints,myints+3);   // assigning from array.
    
      cout << "Size of first: " << int (first.size()) << endl;
      cout << "Size of second: " << int (second.size()) << endl;
      cout << "Size of third: " << int (third.size()) << endl;
      return 0;
    }
    Alrighty So this is an example I found. Helped me understand vectors a bit more.

    First.

    vector<int> first;

    Does this define a vector that only stores integers?

    How can I define one that stores different data types.

    this part needs some explaining.
    Code:
     vector<int>::iterator it;
     it=first.begin()+1;
    What exactly does iterator it mean? I meant it seems like it is a variable related to the vector... not sure exactly

    and lastly if I wanted to input something from a file into a vector what would it look like.

    would it be line

    myIn >> vector<int>::first.begin();

    Something like that idk please help

  10. #10
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    Code:
    std::vector<PlayerInfo>players;
    players.push_back(pi);
    ok the first line defines the vector that stores the info from the structure.

    After <PlayerInfo> what does players do? can you name that anything?

    and push_back(pi);
    That just adds the info in pi to the vector?

    When you say like PlayerInfo pi; What is the name you give to pi. Like what is that called?

  11. #11
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    [quote]
    After <PlayerInfo> what does players do? can you name that anything?
    [quote]

    players is the name of the vector.

    When you say like PlayerInfo pi; What is the name you give to pi. Like what is that called?
    pi is the name. PlayerInfo is the type.

    pi is an instance of a PlayerInfo variable

  12. #12
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472

    Index

    Iterator means counter

  13. #13
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    O ok well that all makes since, now one last question? how can I input from a file and store it in the vector?

    what would the syntax look like?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-03-2008, 11:31 AM
  2. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  3. Functions returning char arrays
    By turmoil in forum C Programming
    Replies: 3
    Last Post: 05-27-2003, 01:43 AM
  4. passing arrays through functions
    By Ecko in forum C++ Programming
    Replies: 4
    Last Post: 04-08-2003, 08:21 PM
  5. Passing multidimensional arrays to functions
    By maxthecat in forum C Programming
    Replies: 3
    Last Post: 12-22-2001, 03:58 PM