reading a text file line by line

This is a stupid question but my memory isn't what it used to be. Ive had this working before but I deleted part of the code. At around line 58-60, I am trying to verify that the lines of text from the text file StudentFile are assigning to the variables FirstName, LastName, etc...by printing it to the screen. It was a simple little snippet but for the life of me, I cant remember how it worked. Please remind me.

This is what the text file looks like:
Harry Houdini 1212 3 MAG101 A PRES201 A ESC301 A
Henry Ford 2211 2 MFG110 A SAL202 B
Albert Einstein 1155 1 CALC100 F
Alan Turing 9082 4 CSC110 A CSC201 A CSC210 A CSC205 A
Melvil Dewey 1876 2 LIBM102 C SUPV202 D


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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <sstream>
#include "Andy.h"
//-----------------------------variables
 
 string LogFileName;
 string TextToWrite;



 string LogTextFile;
//-----------------------------main body
int main()
{
    student MyStudent[20];
	cout << "Welcome to the GradeCaculator Deluxe." << endl;
	
    GetStudentList ();
    OpenClassHours ();
    OpenOutputFile ();
}//end main
//--------GetStudentList
string GetStudentList()
  {
  	student MyStudent[20];
	string FirstName;
    string LastName;
    int StudentId;
    int NumberOfGrades;
    int NumberOfLines = 0;
    string Line;
    string ClassName;
    char Grade;
    int CreditHour;
    int Index = 0; 
	int LineIndex = 0;   
  	ifstream StudentFile;
	student MyStudents;
	grades MyGrades;
	string Course;
	string StudentTextFile;
	cout << "Enter the name of the STUDENT file including the extension :: " << endl;
	getline (cin, StudentTextFile);
    StudentFile.open(StudentTextFile.c_str());
    if(!StudentFile.is_open())
    {
       exit(EXIT_FAILURE);
    }
   while (getline (StudentFile, Line))
   {
   	++NumberOfLines;
   }
//-------------------mine Students.txt for data
    //while (StudentFile >> FirstName >> LastName >> StudentId >> NumberOfGrades)
    while (StudentFile >> FirstName >> LastName >> StudentId >> NumberOfGrades)
    {
    	cout << FirstName << LastName << StudentId << NumberOfGrades << endl;
	}//end while
    
	//	cout << FirstName << LastName << StudentId << NumberOfGrades << endl;
      for (int Index = 0; Index < StudentSize; ++Index)
   {
   	MyStudent[Index].FirstName = FirstName;
   	MyStudent[Index].LastName = LastName;
   	MyStudent[Index].StudentId = StudentId;
   	MyStudent[Index].NumberOfGrades = NumberOfGrades;
   }//end for
   //cout << MyStudent[0].FirstName << endl;
//===============================================
  
//===============================================

	return TextToWrite;
}//end GetStudentList
//--------OpenClassHours
string OpenClassHours()
{
	ifstream HoursFile;
	string ClassHoursTextFile;
	cout << "Enter name of the CLASS HOURS file including the extension :: " << endl;
	getline (cin, ClassHoursTextFile);
	HoursFile.open(ClassHoursTextFile.c_str());
  if(!HoursFile.is_open())
    {
       exit(EXIT_FAILURE);
    }//end if
    string TextToWrite = "Hours.txt successfully opened for reading. \n";
    //OpenLogFile (TextToWrite);
	return TextToWrite;
}//end OpenClassHours
//-------OpenOutputFile
string OpenOutputFile()
{
    ifstream OutputFile;
    string DataTextFile;
    cout << "Enter name of the DATA file including the extension :: " << endl;
	getline (cin, DataTextFile);	
	OutputFile.open(DataTextFile.c_str());
  if(!OutputFile.is_open())
    {
       exit(EXIT_FAILURE);
    }
    TextToWrite = "Data.txt has been successfully opened for reading. \n";	
	OpenLogFile (TextToWrite);
	return TextToWrite;
}//end OpenClassHours
//-------OpenLogFile
string OpenLogFile(string TextToWrite)
{
	
	ofstream OutLogFile;
	OutLogFile.open("Log.txt");
	if (!OutLogFile.is_open())
	{
	 exit(EXIT_FAILURE);	
	}//end if
	 OutLogFile << TextToWrite << endl;
	 OutLogFile.close();
     return TextToWrite;	
}//end OpenLogFile
//-----LookUpGradeValue
int LookUpGradeValue (int Grade)
{
int Hours;
if (Grade == 'A')
{
	Hours = Hours * 4;
}//end ir
if (Grade == 'B')
{
	Hours = Hours * 3;
}//end ir
if (Grade == 'C')
{
	Hours = Hours * 2;
}//end ir
if (Grade == 'D')
{
	Hours = Hours * 1;
}//end ir
if (Grade == 'F')
{
	Hours = Hours * 0;
}//end ir
cout << Hours << endl;
return Hours;
}//end LookUpGradeValue



The loop on line 52 has no effect. Just remove it.

The loop on line 64 is wrong. You get only the very last values since the read loop on line 58 ends before the set loop on line 64 starts.

You need to combine loop 58 and 64:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    int Index = 0;
    while (StudentFile >> FirstName >> LastName >> StudentId >> NumberOfGrades)
    {
    	cout << FirstName << LastName << StudentId << NumberOfGrades << endl;
	}//end while
    
	//	cout << FirstName << LastName << StudentId << NumberOfGrades << endl;
      for (int Index = 0; Index < StudentSize; ++Index)
   {
   	if(Index < StudentSize) // Note
   	{
   	  MyStudent[Index].FirstName = FirstName;
   	  MyStudent[Index].LastName = LastName;
   	  MyStudent[Index].StudentId = StudentId;
   	  MyStudent[Index].NumberOfGrades = NumberOfGrades;
   	  ++Index; // Note
   	}
   	else
   	   break; // Note: Too much data

   }//end whilefor 
Thanks. So the loop on line 52 kinda screws up the reading of the text file because after it ends, it has set it at the end of the file?

I was tryring to use that as a counter to repeat reading through each line of the file. Do I not need to do that to read each line?
ok coder777, I made a few tweaks but I'm not understanding 2 things and how to get there.

Firstly, the next lines of the text file are not being written to the structure, partly I think because that is not the end of the line. The tricky part (no 2 on my list of misunderstood items) is that the text after "NumberofGrades" changes. What I planned to do was repeat a loop "NumberOfGrades" time and assign the following info into the "grades" structure. I am eventually gonna need to match that info with a Hours text file and calculate the GPA based on that comparison.

Any help would be mucho 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <sstream>
#include "Andy.h"
//-----------------------------variables
 
 string LogFileName;
 string TextToWrite;



 string LogTextFile;
//-----------------------------main body
int main()
{
    student MyStudent[20];
	cout << "Welcome to the GradeCaculator Deluxe." << endl;
	
    GetStudentList ();
    OpenClassHours ();
    OpenOutputFile ();
}//end main
//--------GetStudentList
string GetStudentList()
  {
  	student MyStudent[20];
	string FirstName;
    string LastName;
    int StudentId;
    int NumberOfGrades;
    int NumberOfLines = 0;
    string Line;
    string ClassName;
    char Grade;
    int CreditHour;
    int Index = 0; 
	int LineIndex = 0;   
  	ifstream StudentFile;
	student MyStudents;
	grades MyGrades;
	string Course;
	string StudentTextFile;
	cout << "Enter the name of the STUDENT file including the extension :: " << endl;
	getline (cin, StudentTextFile);
    StudentFile.open(StudentTextFile.c_str());
    if(!StudentFile.is_open())
    {
       exit(EXIT_FAILURE);
    }
  // while (getline (StudentFile, Line))
   //{
   	//++NumberOfLines;
   //}
//-------------------mine Students.txt for data
    //while (StudentFile >> FirstName >> LastName >> StudentId >> NumberOfGrades)
    while (StudentFile >> FirstName >> LastName >> StudentId >> NumberOfGrades)
	  {
    	cout << FirstName << " " << LastName << " " << StudentId << " " << NumberOfGrades << endl;
    	for(Index = 0; Index < StudentSize; ++Index) // Note
   	{
   	  MyStudent[Index].FirstName = FirstName;
   	  MyStudent[Index].LastName = LastName;
   	  MyStudent[Index].StudentId = StudentId;
   	  MyStudent[Index].NumberOfGrades = NumberOfGrades;
   	  cout << MyStudent[Index].FirstName << endl;
   	}//end for
	}//end while
 	return TextToWrite;
}//end GetStudentList
//--------OpenClassHours
string OpenClassHours()
{
	ifstream HoursFile;
	string ClassHoursTextFile;
	cout << "Enter name of the CLASS HOURS file including the extension :: " << endl;
	getline (cin, ClassHoursTextFile);
	HoursFile.open(ClassHoursTextFile.c_str());
  if(!HoursFile.is_open())
    {
       exit(EXIT_FAILURE);
    }//end if
    string TextToWrite = "Hours.txt successfully opened for reading. \n";
    //OpenLogFile (TextToWrite);
	return TextToWrite;
}//end OpenClassHours
//-------OpenOutputFile
string OpenOutputFile()
{
    ifstream OutputFile;
    string DataTextFile;
    cout << "Enter name of the DATA file including the extension :: " << endl;
	getline (cin, DataTextFile);	
	OutputFile.open(DataTextFile.c_str());
  if(!OutputFile.is_open())
    {
       exit(EXIT_FAILURE);
    }
    TextToWrite = "Data.txt has been successfully opened for reading. \n";	
	OpenLogFile (TextToWrite);
	return TextToWrite;
}//end OpenClassHours
//-------OpenLogFile
string OpenLogFile(string TextToWrite)
{
	
	ofstream OutLogFile;
	OutLogFile.open("Log.txt");
	if (!OutLogFile.is_open())
	{
	 exit(EXIT_FAILURE);	
	}//end if
	 OutLogFile << TextToWrite << endl;
	 OutLogFile.close();
     return TextToWrite;	
}//end OpenLogFile
//-----LookUpGradeValue
int LookUpGradeValue (int Grade)
{
int Hours;
if (Grade == 'A')
{
	Hours = Hours * 4;
}//end ir
if (Grade == 'B')
{
	Hours = Hours * 3;
}//end ir
if (Grade == 'C')
{
	Hours = Hours * 2;
}//end ir
if (Grade == 'D')
{
	Hours = Hours * 1;
}//end ir
if (Grade == 'F')
{
	Hours = Hours * 0;
}//end ir
cout << Hours << endl;
return Hours;
}//end LookUpGradeValue
Both reading and writing (to the array) must take place in the same loop.

What you are doing now is setting each line read to the whole array. The effect is that at the end the whole array is set with the last line read.

If you want to express it with a for loop you may writ it like so:
1
2
3
4
5
6
7
8
9
    	for(Index = 0; (Index < StudentSize) && (StudentFile >> FirstName >> LastName >> StudentId >> NumberOfGrades); ++Index)
   	{
    	cout << FirstName << " " << LastName << " " << StudentId << " " << NumberOfGrades << endl;
   	  MyStudent[Index].FirstName = FirstName;
   	  MyStudent[Index].LastName = LastName;
   	  MyStudent[Index].StudentId = StudentId;
   	  MyStudent[Index].NumberOfGrades = NumberOfGrades;
   	  cout << MyStudent[Index].FirstName << endl;
   	}//end for 


The tricky part (no 2 on my list of misunderstood items) is that the text after "NumberofGrades" changes.
What does that mean?
Last edited on
tricky part is that I will need to match each course number with a course number in the an hours file and extract the credit hours to multiply to get the GPA.

question in your replay above: what does the and statement do? check to see if the FirstName, LastName, StudentID and NumberOfGrades?

NumberOfGrades will be used to determine hoy many times I need to loop through the last items of each line of text in the students.txt.
question in your replay above: what does the and statement do?
This statement

(StudentFile >> FirstName >> LastName >> StudentId >> NumberOfGrades)

returns true if the data could be correctly read. Otherwise (like eof) it returns false.



I think I see now why the nested loops. You can read the grades like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    	for(Index = 0; (Index < StudentSize) && (StudentFile >> FirstName >> LastName >> StudentId >> NumberOfGrades); ++Index)
   	{
    	cout << FirstName << " " << LastName << " " << StudentId << " " << NumberOfGrades << endl;
   	  MyStudent[Index].FirstName = FirstName;
   	  MyStudent[Index].LastName = LastName;
   	  MyStudent[Index].StudentId = StudentId;
   	  MyStudent[Index].NumberOfGrades = NumberOfGrades;
   	  cout << MyStudent[Index].FirstName << endl;
    	for(int grade_idx = 0; (grade_idx < NumberOfGrades) && (StudentFile >> GradeName >> GradeId); ++grade_idx)
   	{
   	  MyStudent[Index].Grades[grade_idx].Name = GradeName;
   	  MyStudent[Index].Grades[grade_idx].Id = GradeId;
   	}//end for 
   	}//end for  
Thanks for your help. For some reason, it makes sense the way you do it. Maybe you are matching my style..idk. but I do appreciate immensely!
Topic archived. No new replies allowed.