Code keep getting exception thrown error.
Sep 29, 2017 at 3:06am UTC
been trying to run this code which i had some help cleaning up but now it wont run at all and i keep having this "exception thrown" error.
the error that comes up at the bottom of the code is:
Exception thrown at 0x00007FF985F40FC1 (ucrtbased.dll) in CA4.exe: 0xC0000005: Access violation reading location 0x0000000000000000.
*Side note the function called calculateAverage has to have a return type of float and 5 parameters of type float aswell.
Also the calculateLetterGrade has to be a string return type and have one parameter of type float called grade.
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
#include <iostream>
#include <string>
using namespace std;
//make a function that will be called later
float CalculateAverage(float a, float b, float c, float d, float e)
{
int division;
int addition;
addition = (a + b + c + d + e);
division = addition / 5;
return division;
}
string calculateLetterGrade(float grade)
{
int gradeAverage = 0;
{
if (gradeAverage < 50)
return "F" ;
if (gradeAverage <= 54)
return "D-" ;
if (gradeAverage < 58)
return "D" ;
if (gradeAverage < 60)
return "D+" ;
if (gradeAverage < 64)
return "C-" ;
if (gradeAverage < 67)
return "C" ;
if (gradeAverage < 70)
return "C+" ;
if (gradeAverage < 74)
return "B-" ;
if (gradeAverage < 77)
return "B" ;
if (gradeAverage < 80)
return "B+" ;
if (gradeAverage < 85)
return "A-" ;
if (gradeAverage < 890)
return "A" ;
if (gradeAverage >= 90)
return "A+" ;
}
return 0;
}
int main()
{
string letterGrade = 0;
float yourgrade;
int average = 0;
int math = 0;
int english = 0;
int science = 0;
int history = 0;
int geography = 0;
//ask the user to enter 5 of his highschool grades
cout << "Please enter your Math, Science, History and Geography " << endl;
//ask for the Math grade
cout << "Enter your math grade" << endl;
cin >> math;
//ask for the English grade
cout << "Enter your English grade " << endl;
cin >> english;
//ask for the Science grade
cout << "Enter your Science grade " << endl;
cin >> science;
//ask for the History grade
cout << "Enter your History grade " << endl;
cin >> history;
//ask for the Geography grade
cout << "Enter your Geography grade " << endl;
cin >> geography;
//call CalculateAverage function
average = CalculateAverage(math, english, science, history, geography);
//call calculateLetterGrade
letterGrade = calculateLetterGrade(yourgrade);
cout << endl << " ============================================== " << endl;
cout << "Math " << math << "%" << "(" << letterGrade << ")" << endl;
cout << "English " << english << "%" << "(" << letterGrade << ")" << endl;
cout << "Science " << science << "%" << "(" << letterGrade << ")" << endl;
cout << "History " << history << "%" << "(" << letterGrade << ")" << endl;
cout << "Geography " << geography << "%" << "(" << letterGrade << ")" << endl;
cout << endl << "Grade Average " << average << "%" << endl;
cout << endl << " ============================================== " << endl;
system("pause" );
return 0;
}
Last edited on Sep 29, 2017 at 3:14am UTC
Sep 29, 2017 at 4:07am UTC
The exception is comming from line 54:
string letterGrade = 0; the 0 is interpreted as a null pointer.
To construct an empty string, construct it without an initialiser:
string letterGrade;
There are other (logical) errors in the code; some of them have been fixed (and some rough edges smoothened) in this version:
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
#include <iostream>
#include <string>
using namespace std;
//make a function that will be called later
double CalculateAverage(double a, double b, double c, double d, double e)
{
int division;
int addition;
addition = (a + b + c + d + e);
division = addition / 5;
return division;
}
string calculateLetterGrade(double grade)
{
// int gradeAverage = 0;
{
if (grade < 50)
return "F" ;
if (grade <= 54)
return "D-" ;
if (grade < 58)
return "D" ;
if (grade < 60)
return "D+" ;
if (grade < 64)
return "C-" ;
if (grade < 67)
return "C" ;
if (grade < 70)
return "C+" ;
if (grade < 74)
return "B-" ;
if (grade < 77)
return "B" ;
if (grade < 80)
return "B+" ;
if (grade < 85)
return "A-" ;
if (grade < 89)
return "A" ;
if (grade >= 90)
return "A+" ;
}
return 0;
}
int main()
{
// string letterGrade;
// double yourgrade;
// int average = 0;
int math = 0;
int english = 0;
int science = 0;
int history = 0;
int geography = 0;
//ask the user to enter 5 of his highschool grades
cout << "Please enter your Math, Science, History and Geography \n" ;
//ask for the Math grade
cout << "Enter your math grade\n" ;
cin >> math;
//ask for the English grade
cout << "Enter your English grade \n" ;
cin >> english;
//ask for the Science grade
cout << "Enter your Science grade \n" ;
cin >> science;
//ask for the History grade
cout << "Enter your History grade \n" ;
cin >> history;
//ask for the Geography grade
cout << "Enter your Geography grade \n" ;
cin >> geography;
//call CalculateAverage function
const double average = CalculateAverage(math, english, science, history, geography);
//call calculateLetterGrade
const string letterGrade = calculateLetterGrade(average);
cout << '\n' << " ============================================== \n" ;
cout << "Math " << math << "%" << "(" << calculateLetterGrade(math) << ")\n" ;
cout << "English " << english << "%" << "(" << calculateLetterGrade(english) << ")\n" ;
cout << "Science " << science << "%" << "(" << calculateLetterGrade(science) << ")\n" ;
cout << "History " << history << "%" << "(" << calculateLetterGrade(history) << ")\n" ;
cout << "Geography " << geography << "%" << "(" << calculateLetterGrade(geography) << ")\n" ;
cout << '\n' << "Grade Average " << average << "%\n" ;
cout << "\nOverall Letter grade " << letterGrade << '\n' ;
cout << '\n' << " ============================================== \n" ;
// system("pause");
return 0;
}
Last edited on Sep 29, 2017 at 4:11am UTC
Sep 29, 2017 at 4:56am UTC
Thanks alot
i was so confused why it wasn't running it anymore was getting pretty upset so i went to play game for a bit and would come back to it.
Topic archived. No new replies allowed.