Hello, I am getting an error from my code, the program is not displaying the correct grades entered. Please help me find the solution, thank you. This is the code line I think is not working properly: cout << "The grade entered: " << *(characterPointer+i) << endl; Full code:
Hello, I am getting an error from my code, the program is not displaying the correct grades entered. Please help me find the solution, thank you. This is the code line I think is not working properly:
cout << "The grade entered: " << *(characterPointer+i) << endl;
Full code:
#include <iostream>
using namespace std;
char* gradeFinder(int*, int);
double gradeCalculate(int*, int);
int main(int argc, char** argv) {
int studentGrades[25];
int *myPointer = studentGrades;
char *characterPointer;
for (int i = 0; i < 25; i++)
{
cout << "Please enter the students' scores: ";
cin >> studentGrades[i];
while (studentGrades[i] < 0 || studentGrades[i] > 100)
{
cout << "Your entry was not between 0 - 100, please try again: ";
cin >> studentGrades[i];
}
}
characterPointer = gradeFinder(myPointer, 25);
for (int i = 0; i < 25; i++)
{
cout << "The grade entered: " << *(characterPointer+i) << endl;
}
cout << "The average of these grades is: " << gradeCalculate(myPointer, 25);
return 0;
}
char* gradeFinder(int* findGrade, int gradeNumber)
{
char letterGrades[gradeNumber];
char *letterPointer = letterGrades;
for (int i = 0; i < gradeNumber; i++)
{
if (*(findGrade+i) >= 90 && *(findGrade+i) <= 100)
{
letterGrades[i] = 'A';
}
else if (*(findGrade+i) >= 80 && *(findGrade+i) < 90)
{
letterGrades[i] = 'B';
}
else if (*(findGrade+i) >= 70 && *(findGrade+i) < 80)
{
letterGrades[i] = 'C';
}
else if (*(findGrade+i) >= 60 && *(findGrade+i) < 70)
{
letterGrades[i] = 'D';
}
else
{
letterGrades[i] = 'F';
}
}
return letterPointer;
}
double gradeCalculate(int* findGrade, int numberOfGrades)
{
int highest;
int lowest;
double average = 0;
for (int i = 0; i < numberOfGrades; i++)
{
average += *(findGrade+i);
if (*(findGrade+i) > highest) highest = *(findGrade+i);
if (*(findGrade+i) < lowest) lowest = *(findGrade+i);
}
average -= (highest + lowest);
average = average/numberOfGrades;
return average;
}
Thanks again.
Step by step
Solved in 2 steps