Hi I am having an error while running the code below. When I try to find the grade average, the data is not passed properly through the function. I am not sure where I am getting the error, but when I utilize the findGradeAverage function it leads to a negative number error that does not print out properly when I run it. I don't really understand how I get this error, as much as I would like a solution for this school assignment I would like to understand the nature of this problem and how I created it as well so I don't run into this problem in the future. Thanks.
#include <iostream>
#include <iomanip>
using namespace std;
const int MAXGRADE = 25; // maximum number of grades per student
const int MAXCHAR = 30; // maximum characters used in a name
typedef char StringType30[MAXCHAR + 1]; // character array data type for names
// having 30 characters or less.
typedef float GradeType[MAXGRADE]; // one dimensional integer array data type
float findGradeAvg(GradeType, int); // finds grade average by taking array of
// grades and number of grades as parameters
char findLetterGrade(float); // finds letter grade from average given
// to it as a parameter
int main()
{
StringType30 firstname, lastname; // two arrays of characters defined
int numOfGrades; // holds the number of grades
GradeType grades; // grades defined as a one dimensional array
float average; // holds the average of a student's grade
char moreInput; // determines if there is more input
cout << setprecision(2) << fixed << showpoint;
// Input the number of grades for each student
cout << "Please input the number of grades each student will receive." << endl
<< "This must be a number between 1 and " << MAXGRADE << " inclusive"
<< endl;
cin >> numOfGrades;
while (numOfGrades > MAXGRADE || numOfGrades < 1)
{
cout << "Please input the number of grades for each student." << endl
<< "This must be a number between 1 and " << MAXGRADE
<< " inclusive\n";
cin >> numOfGrades;
}
// Input names and grades for each student
cout << "Please input a y if you want to input more students"
<< " any other character will stop the input" << endl;
cin >> moreInput;
while (moreInput == 'y' || moreInput == 'Y')
{
cout << "Please input the first name of the student" << endl;
cin >> firstname;
cout << endl << "Please input the last name of the student" << endl;
cin >> lastname;
for (int count = 0; count < numOfGrades; count++)
{
cout << endl << "Please input a grade" << endl;
cin >> grades[numOfGrades]; // Fill in the input statement to place grade in the array
. }
cout << firstname << " " << lastname << " has an average of ";
cout << findGradeAvg(grades, numOfGrades);// Fill in code to get and print average of student to screen
cout << "Which results in a grade of " << endl;
average = findGradeAvg(grades, numOfGrades);
cout << findLetterGrade(average); // Fill in call to get and print letter grade of student to screen
cout << endl << endl << endl;
cout << "Please input a y if you want to input more students"
<< " any other character will stop the input" << endl;
cin >> moreInput;
}
return 0;
}
//***********************************************************************
// findGradeAvg
//
// task: This function finds the average of the
// numbers stored in an array.
//
// data in: an array of integer numbers
// data returned: the average of all numbers in the array
//
//***********************************************************************
float findGradeAvg(GradeType array, int numGrades)
{
float average = 0.0;
int total = 0;
for (int count = 0; count < numGrades; count++)
{
total += array[count];
average = total / numGrades;
}
return average;
}
//***********************************************************************
// findLetterGrade
//
// task: This function finds the letter grade for the number
// passed to it by the calling function
//
// data in: a floating point number
// data returned: the grade (based on a 10 point spread) based on the
// number passed to the function
//
//***********************************************************************
char findLetterGrade(float numGrade)
{
char letter;
if (numGrade >= 90 && numGrade <= 100)
letter = 'A';
else if (numGrade >= 80 && numGrade <= 89)
letter = 'B';
else if (numGrade >= 70 && numGrade <= 79)
letter = 'C';
else if (numGrade >= 60 && numGrade <= 69)
letter = 'D';
else
letter = 'F';
return letter;
}
Aucun commentaire:
Enregistrer un commentaire