Getting the memory error of c++ exception of char at memory location

I am having trouble with my code compiling. It builds just fine. However when I try to turn it, the code spits out the memory error. I have stepped into the function, and it is storing the vectors where it is suppose to. I have been trying to figure out how to fix this code error. I am not sure how to fix it though.

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
  main.cpp
using namespace std;

int main(int argc, char*argv[]) {
	VS_MEM_CHECK
	string inputFile = argv[1];
	string outputFile = argv[2];
	school readIn = school();
	readIn.run(argv[1], argv[2]);
	return(0);
}


school.cpp
int school::run(string inputFile, string outputFile) {
	//ReadingInputIntoVectors(in);
	
	ifstream in(inputFile);
	if (!in.is_open()) return -1;
	ofstream out(outputFile);
	ReadingInputIntoVectors(in, out);
	OutputtingInfo(out);
	
	
	in.close();//Closing the files
	out.close();
	
	return 0;
};

void school::ReadingInputIntoVectors(ifstream& inputFile, ofstream& outputFile) {   //trying to get the lines to be pushed back into the different vectors :( and it isn't working!!!!
	string newInfo;
	stringstream ss;
	string line;
	//ifstream in;
	outputFile << "Input Strings:" << endl;
		while (getline(inputFile, line)) {
			
			try {
				outputFile << line;
				if (line.length() < 8) {
					throw "Whatever";
				}
				else if ("cr(" == line.substr(0, 3)) {
					string courseName = line.substr(3, line.find(',') - 3);
					line = line.substr(line.find(',') + 1);
					string room = line.substr(0, line.find(')'));
					crInput.push_back(Cr(courseName, room));
				}
				else if ("snap(" == line.substr(0,5)) {
					string studentID = line.substr(5, line.find(',') - 5);
					line = line.substr(line.find(',') + 1);
					string name = line.substr(0, line.find(','));
					line = line.substr(line.find(',') + 1);
					string address = line.substr(0, line.find(','));
					line = line.substr(line.find(',') + 1);
					string number = line.substr(0, line.find(')'));
					snapInput.push_back(Snap(studentID, name, address, number));
				}
				else if ("csg(" == line.substr(0,4)) {
					string course = line.substr(4, line.find(',') - 4);
					line = line.substr(line.find(',') + 1);
					string ID = line.substr(0, line.find(','));
					line = line.substr(line.find(',') + 1);
					string grade = line.substr(0, line.find(')'));
					csgInput.push_back(Csg(course, ID, grade));
				}
				else if ("cdh(" == line.substr(0,4)) {
					string course = line.substr(4, line.find(',') - 4);
					line = line.substr(line.find(',') + 1);
					string dayOfWeek = line.substr(0, line.find(','));
					line = line.substr(line.find(',') + 1);
					string timeOfDay = line.substr(0, line.find(')'));			
					cdhInput.push_back(CdHs(course, dayOfWeek, timeOfDay));
				}
				else {
					throw "Whatever";
				}
			}
				catch (string Sarah) {
					outputFile << Sarah;
				}
				catch (...) {
					outputFile << "Undefined";
				}
				outputFile << endl;
		}
}
void school::OutputtingInfo(ofstream& outputFile) {
	outputFile << endl;
	outputFile << "Vectors:" << endl << endl;

	for (int i = 0; i < snapInput.size(); ++i) {
		outputFile << snapInput[i] << endl;
	}
	for (int j = 0; j < csgInput.size(); j++) {
		outputFile << csgInput[j] << endl;
	}
	for (int k = 0; k < cdhInput.size(); k++) {
		outputFile << cdhInput[k] << endl;
	}
	for (int l = 0; l < crInput.size(); l++) {
		outputFile << crInput[l] << endl;
	}
	outputFile << "Course Grades:" << endl << endl;
	outputFile << "Student Schedules:" << endl << endl;
}
	
school::school() {};
Which line causes the error?

What is the exact error? The exact words and numbers.
Please provide the necessary header files (at a rough guess, school.h) so that your program could be compiled.

Please also provide a sample input file that causes your error.

It is very difficult to debug code that cannot be compiled and run.
I guessed at some headers and input file just to run your code. It seems to run fine, so your problem is probably elsewhere.

Run by:
temp.exe in out

File in:
cr( Maths, C101 )
snap( 12345, The C++ expert, New York, 0101 )
csg( Computer Science, ID-12345, A )
cdh( English, Monday, 9 am )



File out:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Input Strings:
cr( Maths, C101 )
snap( 12345, The C++ expert, New York, 0101 )
csg( Computer Science, ID-12345, A )
cdh( English, Monday, 9 am )

Vectors:

 12345 |  The C++ expert |  New York |  0101 
 Computer Science |  ID-12345 |  A 
 English |  Monday |  9 am 
 Maths |  C101 
Course Grades:

Student Schedules:



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
155
156
157
158
159
160
161
162
163
164
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

// ===== SOME BASIC HEADERS JUST TO GET THE PROGRAM TO COMPILE =====

#define SPACE << " | " <<


struct Cr
{
   string courseName, room;
   Cr( string c, string r ) : courseName( c ), room( r ) {}
};
ostream & operator << ( ostream &strm, Cr cr ) { return strm << cr.courseName SPACE cr.room; }


struct Csg
{
   string course, ID, grade;
   Csg( string c, string i, string g ) : course( c ), ID( i ), grade( g ) {}
};
ostream & operator << ( ostream &strm, Csg csg ) { return strm << csg.course SPACE csg.ID SPACE csg.grade; }


struct CdHs
{
   string course, dayOfWeek, timeOfDay;
   CdHs( string c, string d, string t ) : course( c ), dayOfWeek( d ), timeOfDay( t ) {}
};
ostream & operator << ( ostream &strm, CdHs cdhs ) { return strm << cdhs.course SPACE cdhs.dayOfWeek SPACE cdhs.timeOfDay; }


struct Snap
{
   string studentID, name, address, number;
   Snap( string s, string n, string a, string p ) : studentID( s ), name( n ), address( a ), number( p ) {}
};
ostream & operator << ( ostream &strm, Snap snap ) { return strm << snap.studentID SPACE snap.name SPACE snap.address SPACE snap.number; }


struct school
{
   vector<Cr> crInput;
   vector<Snap> snapInput;
   vector<Csg> csgInput;
   vector<CdHs> cdhInput;

   school();
   int run( string, string );
   void ReadingInputIntoVectors(ifstream& inputFile, ofstream& outputFile);
   void OutputtingInfo(ofstream& outputFile);
};

// ===== END OF ARTIFICIAL HEADER ======================================


int main( int argc, char *argv[]) {
//      VS_MEM_CHECK                      // No idea what this is - so removed it

        string inputFile = argv[1];
        string outputFile = argv[2];
        school readIn = school();
        readIn.run( argv[1], argv[2]);
        return(0);
}

int school::run(string inputFile, string outputFile) {
        //ReadingInputIntoVectors(in);
        
        ifstream in(inputFile);
        if (!in.is_open()) return -1;
        ofstream out(outputFile);
        ReadingInputIntoVectors(in, out);
        OutputtingInfo(out);
        
        
        in.close();//Closing the files
        out.close();
        
        return 0;
}

void school::ReadingInputIntoVectors(ifstream& inputFile, ofstream& outputFile) {   //trying to get the lines to be pushed back into the different vectors :( and it isn't working!!!!
        string newInfo;
//      stringstream ss;
        string line;
        //ifstream in;
        outputFile << "Input Strings:" << endl;
                while (getline(inputFile, line)) {
                        
                        try {
                                outputFile << line;
                                if (line.length() < 8) {
                                        throw "Whatever";
                                }
                                else if ("cr(" == line.substr(0, 3)) {
                                        string courseName = line.substr(3, line.find(',') - 3);
                                        line = line.substr(line.find(',') + 1);
                                        string room = line.substr(0, line.find(')'));
                                        crInput.push_back(Cr(courseName, room));
                                }
                                else if ("snap(" == line.substr(0,5)) {
                                        string studentID = line.substr(5, line.find(',') - 5);
                                        line = line.substr(line.find(',') + 1);
                                        string name = line.substr(0, line.find(','));
                                        line = line.substr(line.find(',') + 1);
                                        string address = line.substr(0, line.find(','));
                                        line = line.substr(line.find(',') + 1);
                                        string number = line.substr(0, line.find(')'));
                                        snapInput.push_back(Snap(studentID, name, address, number));
                                }
                                else if ("csg(" == line.substr(0,4)) {
                                        string course = line.substr(4, line.find(',') - 4);
                                        line = line.substr(line.find(',') + 1);
                                        string ID = line.substr(0, line.find(','));
                                        line = line.substr(line.find(',') + 1);
                                        string grade = line.substr(0, line.find(')'));
                                        csgInput.push_back(Csg(course, ID, grade));
                                }
                                else if ("cdh(" == line.substr(0,4)) {
                                        string course = line.substr(4, line.find(',') - 4);
                                        line = line.substr(line.find(',') + 1);
                                        string dayOfWeek = line.substr(0, line.find(','));
                                        line = line.substr(line.find(',') + 1);
                                        string timeOfDay = line.substr(0, line.find(')'));                      
                                        cdhInput.push_back(CdHs(course, dayOfWeek, timeOfDay));
                                }
                                else {
                                        throw "Whatever";
                                }
                        }
                                catch (string Sarah) {
                                        outputFile << Sarah;
                                }
                                catch (...) {
                                        outputFile << "Undefined";
                                }
                                outputFile << endl;
                }
}
void school::OutputtingInfo(ofstream& outputFile) {
        outputFile << endl;
        outputFile << "Vectors:" << endl << endl;

        for (int i = 0; i < snapInput.size(); ++i) {
                outputFile << snapInput[i] << endl;
        }
        for (int j = 0; j < csgInput.size(); j++) {
                outputFile << csgInput[j] << endl;
        }
        for (int k = 0; k < cdhInput.size(); k++) {
                outputFile << cdhInput[k] << endl;
        }
        for (int l = 0; l < crInput.size(); l++) {
                outputFile << crInput[l] << endl;
        }
        outputFile << "Course Grades:" << endl << endl;
        outputFile << "Student Schedules:" << endl << endl;
}
        
school::school() {}
Topic archived. No new replies allowed.