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.