help with void getData function

This is a discussion on help with void getData function within the C++ Programming forums, part of the General Programming Boards category; Hello all, I am working on a program that will read a text file cpart.dat. I want to use a ...

  1. #1
    Registered User
    Join Date
    Feb 2008

    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. I’m 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:

    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    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)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    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.

    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
    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, 04:45 PM
  4. Pls repair my basketball program
    By death_messiah12 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2006, 04:15 AM
  5. Replies: 5
    Last Post: 02-08-2003, 06:42 PM

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