Unknown debugging errors

closed account (G1qkjzwU)
I've been working on this project for a class for several weeks now with help from my teacher and we worked on this version together so it has all the required elements. Every time I run it in my Visual Studio I get this error: Exception Thrown: read access violation. ***this*** was 0xAA958FFFE8. It opens another file in Visual Studio called xstring and won't let me compile further. The debug always stops after the "Welcome, student" output statement and the program is terminated. However, when my teacher runs it on his device it works fine. Can someone please explain what I'm doing wrong, or maybe some ways that I could write this program better? It has to include all these elements in some way:
Input and output 
Variables 
Arrays 
File I/O  
Iteration (loops) 
Interaction 
Control 

// A program to process user login, convert percentage grade to letter grade and calculate overall GPA
// as well as output the grades, GPA, and personalized suggestion for the user.
// Author: LynneS

#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

int main() {

int USERNAME = 12345;
int PASSWORD = 12345; // variable declarations
int overall_Grade = 0;
char overall_Letter;
float GPA = 0;
bool log_Out;


int grade[10]; // define array
string subject[10];


//initialize array
for (int x = 1; x < 10; x++)
{
grade[x] = 0;
subject[x] = "";
}


cout << "Welcome to the student portal. Please enter your username:" << endl;

do cin >> USERNAME;
while (USERNAME != 12345);
if (USERNAME == 12345)
cout << "Please enter your password." << endl;

do cin >> PASSWORD;
while (PASSWORD != 12345); // login procedure with control and loops
if (PASSWORD == 12345)
cout << "Thank you. Access granted." << endl;

cout << "Welcome, student. Accessing your grades now." << endl;

ifstream infile; // opening grades file
infile.open("12345grades.txt");


int subjects = 0; // determining the number of rows in the file

infile >> subject[subjects]; // reading the first row


do { // loop that will read the grade and subject for the next row

infile >> grade[subjects];
subjects += 1;
infile >> subject[subjects];
} while (subject[subjects] != "0");


for (int x = 0; x < subjects; x++) // summing up the overall grade
{
overall_Grade += grade[x];
}


overall_Grade = overall_Grade / subjects; // determining the overall grade based on number of subjects in the file


// finding overall grade

cout << "Your overall grade is: " << overall_Grade << "%." << endl;

overall_Grade = overall_Grade / 10;

switch (overall_Grade)
{
case 10:
case 9:
overall_Letter = 'A'; // percent to letter grade conversion
break;
case 8:
overall_Letter = 'B';
break;
case 7:
overall_Letter = 'C';
break;
case 6:
overall_Letter = 'D';
break;
default:
overall_Letter = 'F';
break;
}

cout << "Your overall letter grade is: " << overall_Letter << " ." << endl;

switch (overall_Letter)
{
case 'A':
GPA = 4.0;
break;
case 'B':
GPA = 3.0; // letter grade to GPA conversion
break;
case 'C':
GPA = 2.0;
break;
case 'D':
GPA = 1.0;
break;
case 'F':
GPA = 0.0;
break;
}

cout << "Your grade point average (GPA) is: " << GPA << " ." << endl;

if (GPA == 4.0)
cout << "Excellent job! Keep up the great work. Make sure you take some time to relax and have fun sometimes." << endl;
else if (GPA == 3.0)
cout << "Great job! Keep up the great work. You're awesome, just don't be afraid to ask for help if you're struggling." << endl; // personalized suggestions
else if (GPA == 2.0)
cout << "Good job! We both know you can do better. Push yourself a little more, and don't be afraid to ask for help." << endl;
else if (GPA == 1.0)
cout << "I can see that you're struggling. We both know you can do better. Push yourself a little more to succeed and don't be afraid to ask for help. Maybe look into some tutoring." << endl;
else if (GPA == 0.0)
cout << "I can see that you're struggling. We both know you can do better. Push yourself a little more to succeed and don't be afraid to ask for help. Maybe look into some tutoring and talk to your teachers." << endl;

cout << "If you're ready to log out, enter a 1. If not, enter a 0." << endl;

do cin >> log_Out;
while (log_Out == 0); // log out procedure with control and loops
if (log_Out == 1)
cout << "Thank you! Have a great day." << endl;


return 0;
}


Thank you!

Last edited on
check that it actually opens the file and if not, do something other than try to read it.
visual studio is notorious for running the program out of a default folder that is NOT where the source code is; you have to PUT the file in that folder OR run it from the folder where the file is OR put the full path to the file name in the code.
The folder is either debug or release or debug32 or debug64 or some name like that will have projectname.exe
but the best bet is just to use the full path\filename approach.
When posting code, please use code tags so that the code is readable!


[code]
code goes here
[/code]


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

int main() {
	int USERNAME = 12345;
	int PASSWORD = 12345; // variable declarations
	int overall_Grade = 0;
	char overall_Letter;
	float GPA = 0;
	bool log_Out;
	int grade[10]; // define array
	string subject[10];

	//initialize array
	for (int x = 1; x < 10; x++) {
		grade[x] = 0;
		subject[x] = "";
	}

	cout << "Welcome to the student portal. Please enter your username:" << endl;

	do cin >> USERNAME;
	while (USERNAME != 12345);

	if (USERNAME == 12345)
		cout << "Please enter your password." << endl;

	do cin >> PASSWORD;
	while (PASSWORD != 12345); // login procedure with control and loops

	if (PASSWORD == 12345)
		cout << "Thank you. Access granted." << endl;

	cout << "Welcome, student. Accessing your grades now." << endl;

	ifstream infile; // opening grades file
	infile.open("12345grades.txt");

	int subjects = 0; // determining the number of rows in the file

	infile >> subject[subjects]; // reading the first row

	do { // loop that will read the grade and subject for the next row
		infile >> grade[subjects];
		subjects += 1;
		infile >> subject[subjects];
	} while (subject[subjects] != "0");


	for (int x = 0; x < subjects; x++) // summing up the overall grade
	{
		overall_Grade += grade[x];
	}

	overall_Grade = overall_Grade / subjects; // determining the overall grade based on number of subjects in the file

	// finding overall grade
	cout << "Your overall grade is: " << overall_Grade << "%." << endl;

	overall_Grade = overall_Grade / 10;

	switch (overall_Grade) {
		case 10:
		case 9:
			overall_Letter = 'A'; // percent to letter grade conversion
			break;
		case 8:
			overall_Letter = 'B';
			break;
		case 7:
			overall_Letter = 'C';
			break;
		case 6:
			overall_Letter = 'D';
			break;
		default:
			overall_Letter = 'F';
			break;
	}

	cout << "Your overall letter grade is: " << overall_Letter << " ." << endl;

	switch (overall_Letter) {
		case 'A':
			GPA = 4.0;
			break;
		case 'B':
			GPA = 3.0; // letter grade to GPA conversion
			break;
		case 'C':
			GPA = 2.0;
			break;
		case 'D':
			GPA = 1.0;
			break;
		case 'F':
			GPA = 0.0;
			break;
	}

	cout << "Your grade point average (GPA) is: " << GPA << " ." << endl;

	if (GPA == 4.0)
		cout << "Excellent job! Keep up the great work. Make sure you take some time to relax and have fun sometimes." << endl;
	else if (GPA == 3.0)
		cout << "Great job! Keep up the great work. You're awesome, just don't be afraid to ask for help if you're struggling." << endl; // personalized suggestions
	else if (GPA == 2.0)
		cout << "Good job! We both know you can do better. Push yourself a little more, and don't be afraid to ask for help." << endl;
	else if (GPA == 1.0)
		cout << "I can see that you're struggling. We both know you can do better. Push yourself a little more to succeed and don't be afraid to ask for help. Maybe look into some tutoring." << endl;
	else if (GPA == 0.0)
		cout << "I can see that you're struggling. We both know you can do better. Push yourself a little more to succeed and don't be afraid to ask for help. Maybe look into some tutoring and talk to your teachers." << endl;

	cout << "If you're ready to log out, enter a 1. If not, enter a 0." << endl;

	do cin >> log_Out;
	while (log_Out == 0); // log out procedure with control and loops

	if (log_Out == 1)
		cout << "Thank you! Have a great day." << endl;

	return 0;
}

L24 - element access starts at 0, not 1. For an array of size N the elements are 0 to N -1
Somewhat simplified, perhaps:

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
#include <iostream>
#include <fstream>
#include <string>

int main() {
	std::ifstream infile("12345grades.txt");

	if (!infile)
		return (std::cout << "Cannot open file\n"), 1;

	constexpr size_t MaxSub {10};
	constexpr unsigned USER {12345};
	constexpr unsigned PASS {12345};

	unsigned USERNAME {};
	unsigned PASSWORD {};
	unsigned overall_Grade {};
	char overall_Letter {'F'};
	float GPA {};
	unsigned log_Out {};
	int grade[MaxSub] {};
	std::string subject[MaxSub] {};
	size_t subjects {};
	std::string mess;

	std::cout << "Welcome to the student portal.\nPlease enter your username: ";

	do std::cin >> USERNAME;
	while (USERNAME != USER && (std::cout << "Invalid user name. Please re-enter: "));

	std::cout << "Please enter your password: ";

	do std::cin >> PASSWORD;
	while (PASSWORD != PASS && (std::cout << "Invalid password. Please re-enter: "));

	std::cout << "Thank you. Access granted.\n";
	std::cout << "Welcome, student. Accessing your grades now.\n";

	for (; subjects < MaxSub && (infile >> subject[subjects] >> grade[subjects]); ++subjects)
		overall_Grade += grade[subjects];

	std::cout << "Your overall grade is: " << (overall_Grade + .0) / subjects << "%.\n";

	switch (overall_Grade / 10 / subjects) {
		case 10:
		case 9:
			overall_Letter = 'A'; // percent to letter grade conversion
			GPA = 4.0;
			mess = "Excellent job! Keep up the great work. Make sure you take some time to relax and have fun sometimes.\n";
			break;

		case 8:
			overall_Letter = 'B';
			GPA = 3.0;
			mess = "Great job! Keep up the great work. You're awesome, just don't be afraid to ask for help if you're struggling.\n";
			break;;

		case 7:
			overall_Letter = 'C';
			GPA = 2.0;
			mess = "Good job! We both know you can do better. Push yourself a little more, and don't be afraid to ask for help.\n";
			break;

		case 6:
			overall_Letter = 'D';
			GPA = 1.0;
			mess = "I can see that you're struggling. We both know you can do better. Push yourself a little more to succeed and don't be afraid to ask for help. Maybe look into some tutoring.\n";
			break;

		default:
			mess = "I can see that you're struggling. We both know you can do better. Push yourself a little more to succeed and don't be afraid to ask for help. Maybe look into some tutoring and talk to your teachers.\n";
			break;
	}

	std::cout << "Your overall letter grade is: " << overall_Letter << ".\n";
	std::cout << "Your grade point average (GPA) is: " << GPA << ".\n";
	std::cout << mess;
	std::cout << "If you're ready to log out, enter a 1. If not, enter a 0: ";

	do std::cin >> log_Out;
	while (log_Out != 1 && (std::cout << "Enter 1 to log out: "));

	std::cout << "Thank you! Have a great day.\n";
}


which runs OK with VS2022.
closed account (G1qkjzwU)
Thanks for the help! I'm still getting errors when I run it in my visual studio. I'm getting the "Cannot open file" error every time.
Are you running the program from within the IDE? The location of the file you are trying to open is not the directory VS treats as being the local directory.

Copy the file you want to open to the same directory where the source file(s) are located. Now run the program from the IDE, it should open.

If you are running the program from a Windows command prompt, copy the file to the dir where the exe you are running is located.
there is a weird echo in here :)
Really weird echoing now that the OP has decided to cut and run.

At the very least they left their source up so the replies still retain some teaching value.
Topic archived. No new replies allowed.