Average grade ?
This is part a of a 3 part assignment, and im not sure why it isn't returning the correct average.
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
|
#include<iostream>
#include<iomanip>
using namespace std;
bool GetStudent(char Name[]);
int InputData(char Name[][15], double Grade[]);
void OutputData(int N, char Name[][15], double Grade[]);
int main()
{
int Count;
char Name[10][15];
double Grade[10];
Count = InputData(Name, Grade);
OutputData(Count, Name, Grade);
cout << "\n\n";
return (0);
}
int InputData(char Name[][15], double Grade[])
{
int i=0;
while(GetStudent(Name[i]))
{
cout << "What is " << Name[i] << "'s grade? ";
cin >> Grade[i++];
}
return --i;
}
bool GetStudent(char Name[])
{
char YN;
cout << "\nDo you want to enter a new student? (Y/N) ";
cin >> YN;
if(YN > 90)
YN -= 32;
if(YN != 'Y')
return false;
else
{
cout << "\nWhat is the student's name? ";
cin >> Name;
return true;
}
}
void OutputData(int N, char Name[][15], double Grade[])
{
int i;
double sum=0;
cout << "\n";
for(i=0; i<=N; ++i)
{
sum += Grade[i];
}
double avg = sum /N;
cout << setw(16) <<Name[i] << setw(4) << Grade[i] << "\n";
cout <<"\nThe total average is: " << avg;
return;
}
|
What data are you inputting, and what results are you getting?
line 65 change to:
double avg = sum /(N*1.0);
edit:
or the proper way is to static_cast N to a double.
Last edited on
Topic archived. No new replies allowed.