assignment loop/structure help

I can assign the FirstName, LastName & StudentId of a text file to elements in a structure but what I am stuck on is how to loop the program NumberOfGrades times. I know that I need a "for" loop but what is not clicking for me is how to get the "getlin" function to pick up the values from there. I have the for loop commented out because it wasn't working.

//
#include<string>
#include <iostream>
#include <fstream>
#include <cmath>
#include<cstdlib>

using namespace std;
//-----------------------------function declarations
string GetStudentList();
string GetClassHours ();
//-----------------------------create structures
struct student
{
string FirstName;
string LastName;
int StudentId;
int NumberOfGrades;
}MyStudents;
struct Grades
{
string ClassName;
char Grade ;
};



//-----------------------------main body
int main()
{
cout << "Welcome to the GradeCaculator Deluxe." << endl;
GetStudentList ();
GetClassHours ();
// getline(File1) ;
//return 0;
}//end main
//--------GetStudentList
string GetStudentList()

{
string FirstName;
string LastName;
int StudentId;
int NumberOfGrades;
int Index = 0;
int LineIndex = 0;
//Students MyStudents;
ifstream inputFile;
string File1;
//int NumberOfLines = 0;
cout <<"Enter the file name for the list of Students, including the extension::" << endl;
getline(cin, File1);
inputFile.open(File1.c_str());
if(!inputFile.is_open())
{
exit(EXIT_FAILURE);
}
// while (getline (inputFile, File1))
//{
//++LineIndex;
//}
//cout << "Number of Lines in text file is: " << LineIndex << endl;
while (inputFile >> FirstName >> LastName >> StudentId >> NumberOfGrades)
{
MyStudents.FirstName = FirstName;
MyStudents.LastName = LastName;
MyStudents.StudentId = StudentId;
//for (int Index = 0; Index >= NumberOfGrades; ++Index)
//{
// (inputFile >> ClassNumber >> Grade)
//}
}
cout << MyStudents.FirstName << ", " << MyStudents.LastName << ", " << MyStudents.StudentId << endl;

cout << "\nThe file has been successfully opened for reading.\n";

return File1;
}//end GetStudentList
//--------GetClassHours
string GetClassHours()
{
Grades MyHours [15];
ifstream inputFile;
string File2;
cout <<"Enter the file name for the list of Class Hours, including the extension::" << endl;
getline(cin, File2);
inputFile.open(File2.c_str());
if(!inputFile.is_open())
{
exit(EXIT_FAILURE);
}
cout << "\nThe file has been successfully opened for reading.\n";
return File2;
}//end GetClassHours

-----Contents of Students.txt----
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
I think you want
for (int Index = 0; Index <= NumberOfGrades; Index++)

and you won't need this line
int Index = 0;

I can't give the line number because you didn't use code tags.
http://www.cplusplus.com/articles/jEywvCM9/
Replace

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
struct student
{
string FirstName;
string LastName;
int StudentId;
int NumberOfGrades;
}MyStudents;

by

struct student
{
string FirstName;
string LastName;
int StudentId;
int NumberOfGrades;
}MyStudents[10];

or

struct student
{
string FirstName;
string LastName;
int StudentId;
int NumberOfGrades;
}MyStudents*;


Thanks for the info.

Will the getline in the "for" statement pick up where it left off in the "while" statement or will I need to do something different?

Im at work for the next 9 hours so I wont be able to test anything til later but any input will be helpful and save me a lot of time since.

Thanks
Me too I am on work... :)

Basically you assign values to Student[index];
I am still having issues assigning parts of the text file to the structure. I am getting the following error:
[Error] no match for operator []' in MyStudents[index1]'
[Error] no match for operator []' in MyStudents[index2]'

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
//
#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <sstream>
#include "Assignment1.h"
//-----------------------------variables

//-----------------------------main body
int main()
{
	cout << "Welcome to the GradeCaculator Deluxe." << endl;
    GetStudentList ();
}//end main
//--------GetStudentList
string GetStudentList()
  {
	string FirstName;
    string LastName;
    int StudentId;
    int NumberOfGrades;
    string ClassName;
    char Grade;
    int CreditHour;
    int Index = 0; 
	int LineIndex = 0;   
  	ifstream StudentFile;
	student MyStudents;
	grades MyGrades;
	string Course;
	string StudentTextFile;
	string ClassHoursTextFile;
    string DataTextFile;
    int NumberOfLines;
    string Line;
  //  struct student MyStudents[20];
//struct grades MyGrades[15];
//Get file name for the students file and open it
	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);
    }
//Get file name for hours file and open it
	cout << "Enter name of the CLASS HOURS file including the extension :: " << endl;
	getline (cin, ClassHoursTextFile);
    ifstream HoursFile;
	HoursFile.open(ClassHoursTextFile.c_str());
	 if(!HoursFile.is_open())
    {
       exit(EXIT_FAILURE);
    }//end if
//Get output file name and open it
	cout << "Enter name of the DATA file including the extension :: " << endl;
	getline (cin, DataTextFile);
    ifstream OutputFile;
	OutputFile.open(DataTextFile.c_str());
    if(!OutputFile.is_open())
    {
       exit(EXIT_FAILURE);
    }
 //open the log file 
 	ofstream OutLogFile;
	OutLogFile.open("LogFile.txt");
	if (!OutLogFile.is_open())
	{
	 exit(EXIT_FAILURE);	
	}//end if 
//------Get the number of lines in text file for loop control
while (getline(StudentFile, Line))
{
++NumberOfLines;	
}

//-----------------------------------------------------------
    for (int index1 = 0; index1 < NumberOfLines; ++index1)
    {
    	StudentFile >> MyStudents[index1].FirstName 
		            >> MyStudents[index1].LastName
					>> MyStudents[index1].StudentId
					>> MyStudents[index1].NumberOfGrades;
    for (int index2 = 0; index2 < NumberOfGrades; ++index2)
    {
    	StudentFile >> MyStudents[index2].ClassName
    	            >> MyStudents[index2].Grade;
    }//end inner loop
    }//end outter loop
//    while (StudentFile >> FirstName >> LastName >> StudentId >> NumberOfGrades)
//    {
//	MyStudents[index].FirstName = FirstName;
//    MyStudents[index].LastName = LastName;
//    MyStudents[index].StudentId = StudentId;
//    MyStudents[index].NumberOfGrades = NumberOfGrades;
//    for (int Index = 0; Index < NumberOfGrades; ++ Index)
//    {
//    	(StudentFile >> ClassName >> Grade);
////===============================================

////===============================================
//    	while (HoursFile >> Course >> CreditHour)
//         if(Course == ClassName) 
//		 {
//		 	LookUpGradeValue(CreditHour);
//		 	//cout << Hours << endl;
//		 }   	
//    MyGrades.ClassName = ClassName;
//    MyGrades.Grade = Grade;    
//    }//end for
//    }//end while
//    cout << MyStudents.FirstName << ", " << MyStudents.LastName << ", " << MyStudents.StudentId << ", " << MyStudents.NumberOfGrades << endl;
//    cout << MyGrades.ClassName << ", " << MyGrades.Grade << endl;
//    string TextToWrite = "Students.txt successfully opened. \n";
//   // OpenLogFile (TextToWrite);
//	return TextToWrite;
}//end GetStudentList 
Topic archived. No new replies allowed.