How to load file into array of structs line by line?

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

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

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

void greeting ();
int loadData(Item items[]);
void endProgram ();

int main () {


    cout << "Version 1.0" << endl;

    greeting ();

    loadData(items);

    endProgram ();
}

//Be nice and say Hello
void greeting () {
    cout << "Hello, Welcome to Ajay's Grocery!" << endl;
}



int loadData (Item items[]) {
	int currentIndex = 0;
	
	string inFileName;
    ifstream inFile;
	string input;
    cout << "Please enter the pathname to the backup file: ";
    cin >> inFileName;
    inFile.open(inFileName.c_str());
    
	while (!inFile.is_open()) {
        cout << "Please enter input file name (or q to quit): ";
        cin >> inFileName;
        if (inFileName == "q") {
            exit(0);
        }
        inFile.open(inFileName);
    }
    cout << "File opened successfully. " << endl;

	/*while (inFile >> items[currentIndex].code >> setw(26) >> items[currentIndex].name >> items[currentIndex].price ) {	
		cout << "Code = " << items[currentIndex].code << endl;
		cout << "Name = " << items[currentIndex].name << endl;
		cout << "Price = " << items[currentIndex].price << endl;
		currentIndex++;
	}*/
	

	 while (getline(inFile, input)) { 
        
		 items[currentIndex].code = input;
        getline(inFile, input);
        items[currentIndex].name = input;
        getline(inFile, input);
        items[currentIndex].price = atof(input.c_str());
		
		cout << "Code = " << items[currentIndex].code << endl;
		cout << "Name = " << items[currentIndex].name << endl;
		cout << "Price = " << items[currentIndex].price << endl;
        currentIndex++;
    }
	
	 return currentIndex;
}

void endProgram () {
    cout << "Thank you for shopping at Ajay's Grocery! " << endl;
    cout << "Enjoy the day! " << endl;
    system ("Pause");
    exit(1);

	return;
}



I would like to know how to load the input file (shown below) into each slot of the array correctly. Each array index contains a struct, as defined at the top of the file. Once the file is properly loaded into the array, I will have more to do with this project. But first things first.

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

*barcode starts at position 0, description at position 11, and price at position 37.
I figure out how to use the .substr command to parse the inFile strings. I now have the issue of the

while (getline(inFile, input))

loop jumping to the fifth line of the file the second time through the loop. The first time through the loop it parses the string perfectly. The second time through it considers the getline fundtion to be applied to the 5th line of the file.
Topic archived. No new replies allowed.