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;
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;
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.
#include <iostream>
#include <fstream>
#include <cmath>
usingnamespace 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;
elseif (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
elseif (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;
elseif (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;
elseif (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;
}
#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};
constexprunsigned USER {12345};
constexprunsigned 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";
}
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.