How to input a data file to parallel vectors

This is a discussion on How to input a data file to parallel vectors within the C++ Programming forums, part of the General Programming Boards category; I have a data file that can be of any size and the entries look like this: integerID stringName integerNum ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    9

    How to input a data file to parallel vectors

    I have a data file that can be of any size and the entries look like this:

    integerID
    stringName
    integerNum doubleVal1 doubleVal2

    There can be any number of the blocks of data above that need to be parsed into 5 parallel vectors. I know that if I were adding the elements to an array, I would just use a for loop since I would know the array size and add the elements in accordingly. However, since the vectors in parallel can be any size, I do not know how to do it.

    Code:
    void readData(ifstream& inp, int integerID[], string stringName[], int integerNum[], double doubleVal1[], double doubleVal2[])
    {
       inp >> integerID.push_back() >> stringName.push_back() >> integerNum.push_back() >> doubleVal1.push_back() >> doubleVal2.push_back();
    
    }
    I know this doesn't work, but I am not sure how I am supposed to read the data to the vectors and I can't find an example that I understand for the life of me.

    Thanks in advance for any advice.

    Rob

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Is the data for each "block of data" part of a single logical entity? If so then create a struct/class for the entity and then create a single vector of those entities:
    Code:
    class foo
    {
        int ID;
        std::string Name;
        int Num;
        double Val1;
        double Val2;
    public:
        friend std::istream& operator>>(std::istream&,foo&);
    };
    
    std::istream& operator>>(std::istream& is, foo& data)
    {
        return is >> data.ID >> data.Name >> data.Num >> data.Val1 >> data.Val2;
    
    }
    Your read loop then becomes:
    Code:
    foo temp;
    std::vector<foo> fooVector;
    while( inp >> temp )
        fooVector.push_back(temp);
    Or...
    Code:
    #include <algorithm>
    #include <iterator>
    ...
    std::vector<foo> fooVector;
    std::copy(std::istream_iterator<foo>(inp),std::istream_iterator<foo>(),std::back_inserter(fooVector));
    You'll need to add extra member functions to the class as needed.
    Last edited by hk_mp5kpdw; 10-29-2007 at 12:58 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    >> I would just use a for loop since I would know the array size and add the elements in accordingly.
    You can do the same thing with a vector as you would with an array. Set the initial size of the vector with it's constructor:
    Code:
    std::vector<int> integerID(size);
    Then just use a for loop to add into integerID[i] (same for the others).

    I also think the struct idea is a good one if you can do it (which you usually can). You canuse the same technique of setting the size in the beginning if you know it before you start reading.

    If you don't know the size before you start reading in the data (which happens a lot), then just read into temporary variables and push those back.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    Thanks for the input Daved and hk_mp5kpdw I really appreciate it.

    Daved wrote:
    "If you don't know the size before you start reading in the data (which happens a lot), then just read into temporary variables and push those back."

    This happens to be my case. I guess I am trying to do a function like this:
    Code:
    void readData(ifstream& inp, int itemID[], string itemName[], int pOrdered[], double manufPrice[], double sellingPrice[])
    {
    	int ID, Ordered;
    	string Name;
    	double mPrice, sPrice;
    
    	inp >> ID >> Name >> Ordered >> mPrice >> sPrice;
    
    	itemID.push_back(ID);
    	itemName.push_back(Name);
    	pOrdered.push_back(Ordered);
    	manufPrice.push_back(mPrice);
    	sellingPrice.push_back(sPrice);
    }
    Where the vectors are declared in main(). I know this doesn't work, but I hope I'm getting closer! I just need to learn the correct syntax for this function. I get 5 of the errors "C2228: left of '.push_back' must have class/struct/union type is 'int []' "

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Code:
    void readData(ifstream& inp, vector<int>& itemID, vector<string>& itemName)
    I'll let you fix the rest. Notice I made the parameters references, since you will b e modifying the vectors and you want the vectors in main to be updated. (Arrays are automatically passed by reference which is why you didn't have to do that with the arrays.)

    So how do you know when the read finishes? Perhaps your function should return a bool, and you can return false when the read fails. To check that, just check inp.good() after the read (and before any of the push_backs).

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    Thank you Daved. You're a life saver. I was incorrectly passing my vectors. I wish my books would have had some examples of them but the chapters on vectors are very short.

    Thanks again.

    Rob

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    >> wish my books would have had some examples of them but the chapters on vectors are very short.

    Unfortunately this is a shortcoming of most C++ beginner books. Consider getting Accelerated C++ as a refresher, or The C++ Programming Language or The C++ Standard Library as a reference.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    Thanks to Daved's advice, I fixed my function to work properly. Now my problem is calling the function.

    Code:
    void readData(ifstream& inp, vector<int> itemID, vector<string> itemName, vector<int> pOrdered, vector<double> manufPrice, vector<double> sellingPrice)
    {
    	int ID, Ordered;
    	string Name;
    	double mPrice, sPrice;
    	string goodinp;
    
    	inp >> ID >> Name >> Ordered >> mPrice >> sPrice;
    
    while (inp)
    {
    	itemID.push_back(ID);
    	itemName.push_back(Name);
    	pOrdered.push_back(Ordered);
    	manufPrice.push_back(mPrice);
    	sellingPrice.push_back(sPrice);
    	inp >> ID >> Name >> Ordered >> mPrice >> sPrice;	
    }
    
    
    }
    Is said function. I try to call it using this line:

    Code:
    readData(infile, vector<int> itemID, vector<string> itemName, vector <int> pOrdered, vector<double> manufPrice, vector<double> sellingPrice );
    I get errors in passing the parameters.

    Code:
    .\Project.cpp(34) : error C2275: 'std::vector<_Ty>' : illegal use of this type as an expression
            with
            [
                _Ty=int
            ]
    .\ Project.cpp(34) : error C2146: syntax error : missing ')' before identifier 'itemID'
    .\Project.cpp(34) : error C2059: syntax error : ')'
    My two books do not cover this material at all and I'm not sure where to search for help. Is it something small that I am missing or if anyone knows a great place for me to find the answer I would greatly appreciate it.

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    You must pass the vector arguments by reference if you expect them to retain their modified values after the function ends. Otherwise they are passed by copy which mean that changes to them only affect the copies of the vectors as they exist within the function and once the function ends, the original (unmodified) vectors will remain. Also the loop can be cleaned up a bit:
    Code:
    void readData(ifstream& inp, vector<int>& itemID, vector<string>& itemName,
                  vector<int>& pOrdered, vector<double>& manufPrice, vector<double>& sellingPrice)
    {
        int ID, Ordered;
        string Name;
        double mPrice, sPrice;
        string goodinp;
    
        while( inp >> ID >> Name >> Ordered >> mPrice >> sPrice )
        {
            itemID.push_back(ID);
            itemName.push_back(Name);
            pOrdered.push_back(Ordered);
            manufPrice.push_back(mPrice);
            sellingPrice.push_back(sPrice);
        }
    
    }

    .\Project.cpp(34) : error C2275: 'std::vector<_Ty>' : illegal use of this type as an expression
    with
    [
    _Ty=int
    ]
    .\ Project.cpp(34) : error C2146: syntax error : missing ')' before identifier 'itemID'
    .\Project.cpp(34) : error C2059: syntax error : ')'
    There's probably something else going on (in the lines prior to this) that is causing the issue. Please post more code.
    Last edited by hk_mp5kpdw; 10-29-2007 at 07:33 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    Thanks hk_mp5kpdw for the help with the referencing and cleaned up loop.

    Code:
    readData(infile, vector<int> itemID, vector<string> itemName, vector<int> pOrdered, vector<double> manufPrice, vector<double> sellingPrice);
    This is my call of the function above which is producing the errors:
    Code:
    .\Project.cpp(34) : error C2275: 'std::vector<_Ty>' : illegal use of this type as an expression
            with
            [
                _Ty=int
            ]
    .\Project.cpp(34) : error C2146: syntax error : missing ')' before identifier 'itemID'
    .\Project.cpp(34) : error C2059: syntax error : ')'

    Below is all the relevent code that I am having problems with.

    Code:
    include "stdafx.h"
    #include <iostream>
    #include <vector>
    #include <iomanip>
    #include <string>
    #include <fstream>
    
    using namespace std;
    void printHeading();
    void printReport();
    void readData(ifstream &inp,vector<int>, vector<string>,vector<int>,vector<double>,vector<double>);
    
    
    int main()
    {
    vector <int> itemID(0);
    vector <string> itemName(0);
    vector <int> pOrdered(0);
    vector <int> pInStore(0);
    vector <int> pSold(0);
    vector <double> manufPrice(0);
    vector <double> sellingPrice(0);
    
    ifstream infile;
    infile.open("c:\\project.txt");
    if (!infile)
    {
    	cout << "Input file (project.txt) does "
    		  << "not exist." << endl;
    	return 1;
    }
    readData(infile, vector<int> itemID, vector<string> itemName, vector<int> pOrdered, vector<double> manufPrice, vector<double> sellingPrice);
    infile.close();
    infile.clear();
    cout << itemName.size();
    
        return 0;
    }
    
    void readData(ifstream& inp, vector<int>& itemID, vector<string>& itemName, vector<int>& pOrdered, vector<double>& manufPrice, vector<double>& sellingPrice)
    {
    	int ID, Ordered;
    	string Name;
    	double mPrice, sPrice;
    	string goodinp;
    
    
    
    	while (inp >> ID >> Name >> Ordered >> mPrice >> sPrice)
    	{
    		itemID.push_back(ID);
    		itemName.push_back(Name);
    		pOrdered.push_back(Ordered);
    		manufPrice.push_back(mPrice);
    		sellingPrice.push_back(sPrice);
    		
    	}
    
    
    }

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    vector<int> is the type. Pretend your function took all int references instead of vector<int> references. How would you call it? Do the same with the vector<int>.

  12. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    If I pass my parameters as references I get linking errors.

    Code:
    void readData(ifstream& inp, vector<int>& itemID, vector<string>& itemName, vector<int>& pOrdered, vector<double>& manufPrice, vector<double>& sellingPrice)
    If I pass my parameters as values I compile fine but I get the problem where as hk_mp5kpdw stated before
    You must pass the vector arguments by reference if you expect them to retain their modified values after the function ends. Otherwise they are passed by copy which mean that changes to them only affect the copies of the vectors as they exist within the function and once the function ends, the original (unmodified) vectors will remain.
    I've uploaded my errors, but is there something easy that I am not seeing in passing vector arguments as a reference?

    Thanks a ton in advance. I've learned so much from Daved and hk_mw5kpwd so far.

    Rob
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    You get linker errors because the function definitions aren't matching the prototypes. You have plain vectors as parameters in the prototype and vector references in the definition. Fix the prototype to use references as well.

    Calling a function that takes a reference looks exactly the same as calling a function that takes a value. Only the function definition (and prototype) look different, and of course they would act different as well.

  14. #14
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    Thank you Daved. I'll only need one bottle of "Just for Men" to put back the color in my hair I lost from this function. I really appreciate it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem collecting data from input file
    By gkoenig in forum C Programming
    Replies: 12
    Last Post: 03-30-2008, 07:40 AM
  2. ordered linked list
    By redmondtab in forum C Programming
    Replies: 48
    Last Post: 10-22-2006, 06:09 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Writing and modifying data in a file
    By Micko in forum C Programming
    Replies: 2
    Last Post: 02-17-2005, 02:42 AM
  5. reading a columns input data file
    By vk13wp in forum C Programming
    Replies: 6
    Last Post: 04-28-2003, 01:32 PM

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