Programming problem

I am having problem figuring what my next step should be concerning this program I am writing. I am trying to write a program that using "pass by reference" to calculate the average grade for 6 students, and display in this format: Student name -ID, Average and Letter Grade. Now, my problem is how do i create a program that will do this for 6 students (multiple arrays) and how do I write the if statement to display grade each time or is there another or easier way of doing it. I am very new to progamming.....
Here is what I have so far....It works but for only one student at a time and I know its wrong.

#include <iostream>
#include <cstdio>
#include <iomanip>
#include <cmath>
using namespace std;

struct StuRec
{
int id;
int scores [4]; // built in array
double avg;

};// terminate

//pass by value
//double CalcAvg (StuRec);

// pass by reference
void CalcAverage (StuRec& S);

void main ()
{
char stuname[20];


// populate the StudentName using a keyboardcout
cout<<"Enter A Name:";
gets_s(stuname);
cout<< stuname;
cout<<endl;

// declare and allocate variables
StuRec r;

// how to allocate numbers in the record? eg. enter student id

cout<<"Enter ID;";
cin>>r.id;
cout<<r.id;
cout<<endl;


// populate the scores using a keyboard
cout<<"Enter 4 scores: ";
for(int i=0; i<4; i++) // populating the student record r
cin>>r.scores[i];

// compute average
//r.avg = CalcAvg(r); // next step is to make a prototype

// calculate average using
CalcAverage ( r );


cout<<"Average Score: "<<r.avg<<endl;

//if statement

if (r.avg >=90)
cout<<" A: ";
if (r.avg >=80 && r.avg <90)
cout<<"B:";
if (r.avg >=70 && r.avg <80)
cout<<"C:";
if (r.avg >=60 && r.avg <70)
cout<<"D:";
if (r.avg <60)
cout<<"F:";

// Display
cout<< fixed << setprecision(6);
cout<<"Student Name:"<<setw(8)<<stuname<<" ID: "<<setw(8)<<r.id<<"Score: "<<setw(8)<<r.avg<<endl;


}//main

void CalcAverage ( StuRec& S)
{

int sum = 0;
for (int i=0; i<4; i++)
sum+= S.scores[i];

S.avg = static_cast<double>(sum)/4;

}


any help will be highly appreciated.
Thanks

It works but for only one student at a time and I know its wrong
Can you explain a bit more?

And please use [code][/code] tags
Bazzy, what i meant by it works for one student is that, I have only one array set up for one student.

How do I set up multiple array for multiple student so I can keep uploading data without erasing the previous one and all will show on display at the end of my program.

I do not understand what you mean by usinf [code] tag. like i said earlier i am new to this and just trying to learn how it all works.

What I have posted is exactly how I have written the program so far.

Thanks
You can create an array of StuRec and read them in a loop.

- the code tags make the code you post look better: cout << "foobar"; -
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
#include <iostream>
#include <cstdio>
#include <iomanip>
#include <cmath>
using namespace std;

struct StuRec
{
	char	name[64];
	int id;
	int scores [4]; // built in array
	double avg;

};// terminate

//pass by value
//double CalcAvg (StuRec);

// pass by reference
void CalcAverage (StuRec& S);

void main ()
{
	char stuname[20];
	StuRec r[6];

	// populate the StudentName using a keyboardcout
	

	// declare and allocate variables
	int index = 0;
	while(true)
	{
		// how to allocate numbers in the record? eg. enter student id

		cout<<"Enter A Name:";
		cin >> r[index].name;
		cout<<endl;

		cout<<"Enter ID;";
		cin>>r[index].id;
		cout<<r[index].id;
		cout<<endl;


		// populate the scores using a keyboard
		cout<<"Enter 4 scores: ";
		for(int i=0; i<4; i++) // populating the student record r
			cin>>r[index].scores[i];

		// compute average
		//r.avg = CalcAvg(r); // next step is to make a prototype

		// calculate average using
		CalcAverage ( r[index] );
		index++;

		char more;
		std::cout << "More (y/n)" << std::endl;
		cin >> more;
		if(more != 'y')
			break;
		
	}

	for(int i = 0; i < index; i++)
	{
		//if statement
		//cout<<"Average Score: "<<r[i].avg<<endl;

		if (r[i].avg >=90)
			cout<<" A: ";
		if (r[i].avg >=80 && r[index].avg <90)
			cout<<"B:";
		if (r[i].avg >=70 && r[index].avg <80)
			cout<<"C:";
		if (r[i].avg >=60 && r[index].avg <70)
			cout<<"D:";
		if (r[i].avg <60)
			cout<<"F:";

		// Display
		cout<< fixed << setprecision(6);
		cout<<"Student Name:"<<setw(8)<<r[i].name<<" ID: "<<setw(8)<<r[i].id<<"Score: "<<setw(8)<<r[i].avg<<endl;
	}
	


}//main

void CalcAverage ( StuRec& S)
{

	int sum = 0;
	for (int i=0; i<4; i++)
	sum+= S.scores[i];

	S.avg = static_cast<double>(sum)/4;

}
Topic archived. No new replies allowed.