Cash Register program load data function

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.
////////////////////////////////////////////////////////////////////////////

For the first function defined in the instructions I am unsure of how to attempt. My function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  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 then print error message
    	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.
Last edited on
Reading a text file line by line is a common task. It's worth to learn this first and also keep a code snippet for future use.
Here is a little example how to read a file line by line. Have a look maybe you can adapt it to your needs.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <fstream>
#include <stdlib.h>

using namespace std;


int main ()
{
  #define FILENAME  "C:\\Temp\\Questions.txt"

  ifstream input(FILENAME);

  if (!input)
  {
    cout << "Error opening file";
    exit(1);
  }
  char buffer[128];
  int line_count = 0;
  while(input.getline(buffer, sizeof(buffer)))
  {
    // do sth. with buffer here
    cout << buffer << endl;
    line_count++;
  }
  cout << endl << line_count << " lines read." << endl;

  system("pause");
} 


Make sure that you understand this first before you try to get ahead with your task.
Adding to this comprehensive advise:

Here is the tutorial page that helps explain how to input into a file and extract things into a program from a file:
http://www.cplusplus.com/doc/tutorial/files/

I hope this helps.

- Hirokachi
Topic archived. No new replies allowed.