record structures & arrays
Nov 10, 2009 at 12:31am UTC
My latest cpp assignment. i feel like i'm getting close, but i could definitely use some advice. ERROR on line 75.
Write a program that uses a record structure to store
a Student Name, Student ID, Test Scores, Average Test
Score, and Grade. The program should keep a list of
test scores for a group of 6 students. The program
should ask for the name, ID, and four test scores for
each student. Then the average test score should be
calculated and stored. The course grade should be
based on the following scale:
Average Test Score Course Grade
------------------ ------------
90 - 100 A
80 - 89 B
70 - 79 C
60 - 69 D
59 or below F
A table should be displayed on the screen listing each
student's name, ID, average test score, and course grade.
Implement with functions.
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
#include <iostream>
using namespace std;
const int columns = 4;
struct StuRec //user defined datatype
{
int id[6];
char names[6][20];
int scores[6][4];
double avg[6];
char grade[6];
}; //semi-colon required
//pass by reference
void calculateAverage(StuRec&);
char letterGrade(StuRec&);
int main()
{
const int rows = 6;
//declare and allocate
StuRec r;
//enter student id
cout<<"Student id: " ;
for (int i=0; i<6 ;i++)
cin>>r.id[i];
//enter student+scores
for (int i=0; i<6 ;i++)
{
cout<<"Enter Name of student " <<i+1<<": " ;
cin>>r.names[i];
cout<<endl;
}
for (int i=0; i<rows; i++)
{
cout<<"Enter " <<columns<<" scores for student " <<i+1<<": " ;
for (int j=0; j<columns; j++)
{
cin>>r.scores[i][j];
}
cout<<endl;
}
//display
cout<<"Student id: " <<r.id<<endl;
cout<<"Scores: " ;
for (int i=0; i<4 ; i++)
cout<<r.scores<<" " ;
cout<<endl;
//compAvg
calculateAverage(r);
cout<<"Avg Score: " <<r.avg<<endl;
letterGrade(r);
cout<<"Letter Grade: " <<r.grade<<endl;
}//endmain
void avg(StuRec& r)
{
int sum=0;
for (int i=0; i<6 ; i++)
sum+=r.scores[columns][i];
r.avg=static_cast <double >(sum)/4;
//error: incompatible types in
//assignment of 'double' to 'double [6]'
//
}
char grade(StuRec& r)
{
int sum = 0;
char grade;
for (int j=0; j<6; j++)
{
sum += r.scores[columns][j];
}
double avg = sum/4;
cout<<"Avg: " <<avg;
if (avg >= 90)
grade = 'A' ;
if (avg >= 80 && avg < 90)
grade = 'B' ;
if (avg >= 70 && avg < 80)
grade = 'C' ;
if (avg >= 60 && avg < 70)
grade = 'D' ;
if (avg < 60)
grade = 'F' ;
return grade;
}
Nov 10, 2009 at 12:34am UTC
On line 11 you declare avg as:
double avg[6];
but you are trying to use it like it is just a single double
.
Nov 10, 2009 at 1:01am UTC
okay now it runs, but it isnt returning anything(readable/correct) and i have no clue as to why.
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
#include <iostream>
using namespace std;
const int columns = 4;
struct StuRec //user defined datatype
{
int id[6];
char names[6][20];
int scores[6][4];
double avg[6];
char grade[6];
}; //semi-colon required
//pass by reference
void avg(StuRec&);
char grade(StuRec&);
int main()
{
const int rows = 6;
//declare and allocate
StuRec r;
//enter student id
for (int i=0; i<6 ;i++)
{
cout<<"Enter Student id " <<i+1<<": " ;
cin>>r.id[i];
}
cout<<endl;
//enter student+scores
for (int i=0; i<6 ;i++)
{
cout<<"Enter Name of student " <<i+1<<": " ;
cin>>r.names[i];
cout<<endl;
}
for (int i=0; i<rows; i++)
{
cout<<"Enter " <<columns<<" scores for student " <<i+1<<": " ;
for (int j=0; j<columns; j++)
{
cin>>r.scores[i][j];
}
cout<<endl;
}
//display
cout<<"Student id: " <<r.id<<endl;
cout<<"Scores: " ;
for (int i=0; i<4 ; i++)
cout<<r.scores<<" " ;
cout<<endl;
//compAvg
avg(r);
cout<<"Avg Score: " <<r.avg<<endl;
grade(r);
cout<<"Letter Grade: " <<r.grade<<endl;
}//endmain
void avg(StuRec& r)
{
double a;
double sum=0;
for (int i=0; i<6; i++)//loop thru students
{
a=sum/4;
r.avg[i]=a;
cout<<endl;
}
}//end avg
char grade(StuRec& r)
{
int sum = 0;
char grade;
for (int j=0; j<6; j++)
{
sum += r.scores[columns][j];
}
double avg = sum/4;
cout<<"Avg: " <<avg;
if (avg >= 90)
grade = 'A' ;
if (avg >= 80 && avg < 90)
grade = 'B' ;
if (avg >= 70 && avg < 80)
grade = 'C' ;
if (avg >= 60 && avg < 70)
grade = 'D' ;
if (avg < 60)
grade = 'F' ;
return grade;
}
Last edited on Nov 10, 2009 at 1:01am UTC
Topic archived. No new replies allowed.