Not averaging correctly?

My program isnt averaging correctly.


#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;

struct nameGPA
{
string name; //name of student
int idNUM; //student id
int test[5]; //array of test that stores values 5 values
double average; // average of test score
char grade; //grade for test scores
};

//Function prototypes

void getData(nameGPA *&, int); //get info prototype
void displayR(nameGPA*&, int); //display prototype



int main()
{

nameGPA *roster; //pointer to roster
int num; // int num


cout << "How many students? "; //cout how many students
cin >> num; // save how many students

getData(roster, num); //call the function of roster, aka nameGPA
displayR(roster, num); // call the display function to display






system("pause"); // pause the system
return 0;


}

// here we define the functions

void getData(nameGPA *&c, int number)
{
c = new nameGPA[number]; // make new array that stores info from the structure above named nameGPA

int i = 0; // initialize int i for loop to repeat process
double total = 0; // total for finding average
for (i = 0; i < number; i++) //start loop to loop as many times as entered in cin >>
{
cout << " Enter info for student " << (i + 1) << " here" << ":"; //displays and addes 1 to each
cout << endl;

cout << " Student name: "; // get student name
cin.ignore();
getline(cin, c[i].name);

cout << " What is " << c[i].name << "'s ID number? "; // get id number
cin >> c[i].idNUM;

if (c[i].idNUM < 0) // input validation for id number
{
cout << " ID number are positive, please enter a positive ID: ";
cin >> c[i].idNUM;

}

cout << " Enter 5 Test scores for " << c[i].name << endl; // display to enter 5 test scores

for (int k = 0; k < 5; k++) // loop for 5 test scores for each student to start
{

cout << " Test score" << (k + 1) << ": "; // add a 1 after test "score" , it makes it store1:, store2: etc
cin >> c[i].test[k]; // cin and store test scores in array of 5 for each nameGPA ( roster )

while (c[i].test[k] < 0) // input validation for test scores
{
cout << " Enter positive numbers test score: ";
cin >> c[i].test[k];

}
}



for (int j = 0; j < 5; j++) // loop to find average of each student entered

{

total += c[i].test[j]; // makes total


}

c[i].average = total / 5; //calculates average
// letter grade assigned for each average
if (c[i].average >= 90 && c[i].average <= 100)
c[i].grade = 'A';
else if (c[i].average >= 80 && c[i].average <= 90)
c[i].grade = 'B';
else if (c[i].average >= 70 && c[i].average <= 80)
c[i].grade = 'C';
else if (c[i].average >= 60 && c[i].average <= 70)
c[i].grade = 'D';
else if (c[i].average > 0 && c[i].average <= 60)
c[i].grade = 'F';



}





}






// we display the results here
void displayR(nameGPA*&c, int y)
{
cout << " Name" << " " << "ID number" << " 5 Test Scores " << "Average" << " " << "Grade" << endl;
cout << "--------------------------------------------------------------------------" << endl;

for (int i = 0; i < y; i++)
{

cout << setw(13) << left << c[i].name;
cout << setw(4) << right << c[i].idNUM;
for (int j = 0; j < 5; j++) // display results of test scores here entered for each student ( it loops till for 5 test scores )
{

cout <<" "<< setw(4.5) << right << c[i].test[j];

}
cout << setw(6) << right <<" " <<c[i].average;
cout << setw(10) << right << c[i].grade << endl;


}




}
In your getData function, initialize the variable total inside of the for loop instead of outside.
ive tried it, and it still gives me the wrong average.
1
2
3
4
5
double total = 0;	// total for finding average // here is the mistake
for (i = 0; i < number; i++) //start loop to loop as many times as entered in cin >> 
{
cout << " Enter info for student " << (i + 1) << " here" << ":"; //displays and addes 1 to each 
cout << endl;


every time previous total add with current total so if you look with care 1st time avg is correct but then after incorrect.

correct coding is:


1
2
3
4
5
for (i = 0; i < number; i++) //start loop to loop as many times as entered in cin >> 
{
double total = 0;	// total for finding average //change line possition
cout << " Enter info for student " << (i + 1) << " here" << ":"; //displays and addes 1 to each 
cout << endl;



change line 1 and line 2

enjoy your code now.
Last edited on
THankx for both of you guys help! much appreciated!
Topic archived. No new replies allowed.