Reading Records from file data.txt into Array of Structs

For this assignment, I'm trying to read records from a text file I made ("data.txt.txt") and read it into the array of structs.

For this assignment you are to read all records from a file named data.txt into an appropriately named and constructed array of structs. You will not know exactly how many records there will be in the file but you do know that there will be no more than 1000. The data.txt file will consist of records of the following format:
Sales person ID (string), item1 ID # (string), item1 quantity (integer), item1 price (double), item2 ID # (string), item2 quantity (integer), item2 price (double), item3 ID # (string), item3 quantity (integer), item3 price (double), item4 ID # (string), item4 quantity (integer), item4 price (double), order date (string -- format: mm/dd/yyyy), contact email(string)
Once all records are read into the array of structs, sort the array in ascending order based on order date.


I can't seem find the problem in my current code...Any help would be greatly appreciated.

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
98
99
100
101
102
103
104
105
106
#include <iostream>
#include <fstream>
#include <string>
using namespace std;



struct salesPerson
	{
	    string name, item1, item2, item3, item4, orderDate, email;
		int	itemQ1, itemQ2, itemQ3, itemQ4, calcField;
		double	itemP1, itemP2, itemP3, itemP4;
    	
	}; 
  
struct salesPerson sort(struct salesPerson lstP[], int size) {
    //this routine does the soring of the structure array based on the orderDate
    //you can use brute force sorting here
    //do a inplace bubble sort based on the order date
    return lstP;
}


int main ()
{
  struct salesPerson listsalesPerson[1000]; //should be set to 1000 since the number of records will be less than 1000
  struct salesPerson sortedsalesPerson[1000];
  int current_position = 0;  
  string name, data_type, item1, itemQ1, itemP1, item2, itemQ2, itemP2, item3, itemQ3, itemP3, item4, itemQ4, itemP4, orderDate, email;	
  
  ifstream inFile;
  inFile.open("data.txt.txt");
  
  //Check for Error
	if (!inFile.is_open())
	{
		cerr << "Error Opening File" << endl;
		exit (1);
	}
  
  
 //while you haven't reached the end of the file
 //{
 // read the line(readline or getline)
 // split line and get the name, item, itemquantity from the line say
 //listsalesPerson[current_position].name = name you extracted above
 //listsalesPerson[current_position].item = item you extracted above 
 //listsalesPerson[current_position].itemQuantity = itemQuantity you extracted above typecasted to int
 //and so on for other elements extracted above
 // current_position++

  while (!inFile.eof())
{	
	getline(inFile, name,' ');
    getline(inFile, data_type,' ');
    //getline for item
    //getline for item data_type
    
    getline(inFile, item1, ' ');
    getline(inFile, itemQ1, ' ');
	getline(inFile, itemP1, ' ');

	getline(inFile, item2, ' ');
	getline(inFile, itemQ2, ' ');
	getline(inFile, itemP2, ' ');

	getline(inFile, item3, ' ');
	getline(inFile, itemQ3, ' ');
	getline(inFile, itemP3, ' ');

	
	getline(inFile, item4, ' ');
	getline(inFile, itemQ4, ' ');
	getline(inFile, itemP4, ' ');

    getline(inFile, orderDate, ' ');
	getline(inFile, email, ' ');
	
    
    
    listsalesPerson[current_position].name = name; //you would have to typecase it in the data_type.
	listsalesPerson[current_position].item1 = item1;
	listsalesPerson[current_position].itemQ1 = atoi(itemQ1.c_str());
	listsalesPerson[current_position].itemP1 = atoi(itemP1.c_str());
	listsalesPerson[current_position].item2 = item2;
	listsalesPerson[current_position].itemQ2 = atoi(itemQ2.c_str());
	listsalesPerson[current_position].itemP2 = atoi(itemP2.c_str());
	listsalesPerson[current_position].item3 = item3;
	listsalesPerson[current_position].itemQ3 = atoi(itemQ3.c_str());
	listsalesPerson[current_position].itemP3 = atoi(itemP3.c_str());
	listsalesPerson[current_position].item4 = item4;
	listsalesPerson[current_position].itemQ4 = atoi(itemQ4.c_str());
	listsalesPerson[current_position].itemP4 = atoi(itemP4.c_str());
	listsalesPerson[current_position].orderDate = orderDate;
	listsalesPerson[current_position].email = email;

	current_position++;
}

//sortedsalesPerson = sort(listsalesPerson, current_position+1);
  
  inFile.close();
  

  return 0;
}
Topic archived. No new replies allowed.