error exe has stopped working

Can someone tell me why tis is producing an "exe has stopped working error?

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
#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 = 0;
    string ClassName;
    char Grade;
    int CreditHour;
    ifstream StudentFile;
	string Course;
	string StudentTextFile;
	string ClassHoursTextFile;
    string DataTextFile;
    int NumberOfLines = 0;
    string Line;
    const int StudentRecs = 20;
    student MyStudents[StudentRecs];
    const int GradeRecs = 15;
    grades MyGrades[GradeRecs];
//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;	
}//end while
cout << NumberOfLines << endl;
//-----------------------------------------------------------
StudentFile.open(StudentTextFile.c_str());

    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)
    {
    	HoursFile >> MyGrades[index2].ClassName
    	            >> MyGrades[index2].Grade;
    }////end inner loop
  }//end outter loop
cout << MyStudents[0].FirstName << endl;
}//end GetStudentList

//-----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;
//}


header file

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
using namespace std;
#include <iostream>
#include <string>
#include <fstream>
#include <string>

//-----build structures
struct student
{
    string FirstName;
    string LastName;
    int StudentId;
    int NumberOfGrades;
};

struct grades
{
string ClassName;
char Grade;
};

//-----Variables

//-----function declarations
//void GetLogFileName (string&);
//string OpenLogFile (string);
//int OpenLogFile (ofstream&, string&, string);
//void WriteTextToLogFile (string);
string GetStudentList();
//string OpenClassHours ();
//string OpenOutputFile ();
//int LookUpGradeValue (int); 


Last edited on
Possible reason is myStudent array index out of bound, beause size of myStudent and numberOfLine handled seperately, its possible for numberOfLine exceed size of myStudent
It still happens if I comment out the array sizes and the part that assigns to the array
:(
What about myGrades and numberOfGrades
And n which part the program stop workng ?
It crashes right after "cout << NumberOfLines << endl;"

And everything after has been commented out.
http://stackoverflow.com/questions/2284775/c-can-i-reuse-fstream-to-open-and-write-multiple-files
They're talking about ofstream there, maybe its also applied for ifstream too, sicce you're using StudentFile.open twice, and you said program stop workin in that lne too
still crashing
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
//
#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 = 0;
    string ClassName;
    char Grade;
    int CreditHour;
    ifstream StudentFile;
	string Course;
	string StudentTextFile;
	string ClassHoursTextFile;
    string DataTextFile;
    int NumberOfLines = 0;
    string Line;
    //const int StudentRecs = 20;
    //student MyStudents[StudentRecs];
    //const int GradeRecs = 15;
    //grades MyGrades[GradeRecs];
//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;	
}//end while
cout << NumberOfLines << endl;
//-----------------------------------------------------------
//StudentFile.open(StudentTextFile.c_str());
//
//    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)
//    {
//    	HoursFile >> MyGrades[index2].ClassName
//    	            >> MyGrades[index2].Grade;
//    }////end inner loop
//  }//end outter loop
//MyStudents[0].FirstName << endl;
}//end GetStudentList 
On the same line ? After endl ? Even after turn may line into a comment ?
How about explicitly close the ftream before the function end ? I wsh I could try it myself but currently i'm not bring my laptop
Topic archived. No new replies allowed.