Problem with Reading Input File using struct Variable

Hi,

I am having problem with getting input from text file and display the output in console. My coding works like this. First it reads input from text file and store them in struct variable using array. Then, by using the same variable, it will write the output in the console. I presume the problem is the code did not work well when reading the input. Thus, it produces incorrect result. So here, I attach the input file, codes, and the produced output. Pls help me fixing this cause I was stuck in trying to fix this the whole day. Thanks!

Input file : courseList.txt
201000101 Christopher Ng; CSC110; 1 CTU101; BEL120; CSC128; CSC118; MAT183;
201100210 James Woo; CSC113; 2 MAT183; CSC128; BEL130; MAT112; CTU103;
201000311 Nurul Izah Jabbar; CSC110; 1 CTU101; BEL120; CSC128; CSC118; MAT183;
201000548 Nadia Akmal Ali; CSC110; 3 CTU211; BEL311; CSC238; CSC253; MAT183;
201105253 Azizul Hakim Jamal; CSC110; 4 CSC204; CSC248; CSC318; ITS232; MAT183;

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
struct Course
{
	int studentID;
   char name[30];
   char progCode[10];
   int semester;
   char courseCode[5][10];
};

int main()
{
	ifstream in;
   ofstream out1, out2, out3;
   Course course[5];

   in.open("courseList.txt");
   out1.open("CSC110.txt");
   out2.open("CSC113.txt");
   out3.open("CSC110MAT183.txt");

    //(a)
   if (in.fail())
   {
   	cout << "File does not exist." << endl;
      return 0;
   }



   //(b)
   for (int i=0; i<5; i++)
   {
   	in >> course[i].studentID;
      in.getline(course[i].name,29,';');
      in.getline(course[i].progCode,6,';');
      in >> course[i].semester;
      for (int j=0; j<5; j++)
      	in.getline(course[i].courseCode[j],6,';');
   }

   for (int i=0; i<5; i++)
   {
   	cout << course[i].studentID;
      cout << course[i].name;
      cout << course[i].progCode;
      cout << course[i].semester;
      for (int j=0; j<5; j++)
      	cout << course[i].courseCode[j];
      cout << endl;
   }
   in.close();
   out1.close();
   out2.close();
   out3.close();
   getch();
   return 0;
}


201000101 Christopher Ng CSC110 1 CTU101 BEL120
00
208995052034078760
18337510400
-12089922080
Last edited on
in >> course[i].semester; Where in your input is this?

201000101 Christopher Ng; CSC110; 1 CTU101; BEL120; CSC128; CSC118; MAT183;
201000101 Christopher Ng; CSC110; 1 CTU101; BEL120; CSC128; CSC118; MAT183;

The one that I bold is the value for course[i].semester.
As far as i could see that there is no shifting in lines ... e.g. you did not change the location of your file pointer.

At the end of your //b for loop you should shift the location of file stream otherwise it will point to same location.
I dont understand how can I shift the location of file stream? Pls give me some example.
Last edited on
Topic archived. No new replies allowed.