1

I have this homework assignment that has me a little lost. I have been working on this assignment writing my code, erasing, rewrite, repeat etc. Here are the instructions:


Step (1)

Associated with this assignment is a data file called prices.txt. It contains details of the prices for items in a grocery store. The first column is the barcode for the item (http://en.wikipedia.org/wiki/Barcode) the second column, starting in position 11, is the product name, and the third column, starting in position 37 is the product price. Write a function with the signature int loadData() which prompts the user for the location of the data file on disk and then loads the data from the file into an array of elements. Each element of the array should be a struct declared as follows:

struct Item {
string code;
string name;
double price;
};

The array itself should be called items and should be declared at file scope as follows:

const int MAX_ITEMS = 100;
Item items[MAX_ITEMS];

Notice that the array has size 100, so you can assume the number of items in the data file is less than 100.

Therefore, when loadData has finished loading the data into the array some elements in the array will be unused. The function loadData returns the number of items actually loaded into the array.


I am unsure on how to attempt the function described in the instructions. My code:

int loadData () {
        string inputFileName;
        ifstream inputFile;
        int numOfItems = 0;

        cout << "Please input the name of the backup file: ";
        cin >> inputFileName; //read user input for the location of the 
        //backup file
        inputFile.open(inputFileName.c_str()); //open specified document

        if (!inputFile.is_open()) { //If the file does not open the errormsg
            cout << "Unable to open input file." << endl;
            cout << "Press enter to continue...";
            getline(cin, reply);
            exit(1);
        } 
        //Not sure where to start. I know I need to get each element from
         //each newline.

        return numOfItems;  
    }

I wanted to figure this out on my own but that isn't gonna happen. So if I could just get some hints or even suggested pools of knowledge that would guide me or even give me an idea of where to start.

addition: input file:

10001     Apples (bunch)            4.59
10002     Bananas (bunch)           4.99
10003     Pears (bunch)             5.49
20001     White bread (loaf)        2.69
20002     Brown bread (loaf)        2.89
20003     English Muffins (bag)     3.99
30001     Sugar (5 lb bag)          3.99
30002     Tea (box)                 4.29
30003     Folger's Coffee (Can)     13.29

This is the entire input file.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
B T Gingy
  • 15
  • 6

1 Answers1

0

Since the input file seems to be using fixed-width columns, it's actually very easy to extract the fields. Just read one line at a time, and for each line get each element as a sub-string, and put into the structure members. And there are functions to convert strings to floating-point values as well.

Don't worry about the possible leading or trailing spaces, there are ways of trimming that.

Community
  • 1
  • 1
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Ok so I have used your suggestions, I read it in one line at a time and broke each line into substrings. What I am not understanding now is how to set these values to the elements. If I have an array items[100] of type struct item. (item items [100]) how would I take my new substrings and laod them into this array? I tried "items.member_name = corresponding_substring" but i keep getting this error... "request for member 'member_name' in 'items' which is of non-class type 'Item[100]" I'm not sure what do to. – B T Gingy Nov 25 '15 at 19:00