Thread: help with void getData function

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    9

    help with void getData function

    Hello all,
    I am working on a program that will read a text file cpart.dat. I want to use a void getData function that I can call in main. Im having problems to say the least. Here is what I have so far, also: I'm thinking I want to use push_back function in while (inp), to add data to my vectors. Any help would be greatly appreciated

    Here is what I have so far:

    Code:
    void getData(ifstream& inp, vector<int>& itemIDs, vector<string>& itemNames,
                vector<int>& pOrdered, vector<int>& pInStore,
                vector<int>& pSold, vector<double>& manufPrice,
                vector<double>& sellingPrice)
    {
        int id;
        string name;
        int itemsOrdered;
        double mPrice;
        double sellPrice;
        char ch;
    
        inp >> id;                                        // Read item id
        inp.get(ch);                                    // Read a newline
        getline(inp, name);                                // Read item name
        inp >> itemsOrdered >> mPrice >> sellPrice;        // Read other info of the item
    
        while (inp)
    
        {
                                                        // read the remaining items
        }
    
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    1.better make the function return int - so you can indicate success/failure to the calling function
    2. I'd make the struct that includes all the fields needed and fill the struct - in this case I'd have only 1 vector of structs instead of bunch of vectors.
    3. Of course the next step will be to make a struct class to add some functionality to it (like reading/writing all members with jsut one line of code)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I wholeheartedly agree with vart's suggestion: Make it a class, create a read function in the class [and make that one a "all or nothing" - meaning that you either successfully read ALL the data for the struct, or you get "nothing". This means a bit of extra checking to see that you are reading the data correctly, but well worth it the first time you get a "bad" data-file.

    Then it's very easy to build a loop to push the data into vector of that class.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    9
    Thanks for the replies, I'm not too familar with structs. but I'll look into it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Pls repair my basketball program
    By death_messiah12 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2006, 05:15 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM