Compiling

Guys i have this code that is handling structures, but is giving some weird outputs
Can anyone have a look and tell me whats wrong

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
#include <iostream>
#include <cmath>

using namespace std;

struct student
{
	string firstName, lastName;
	int ID;
	double testScores[5]; 
	double GPA;
};

void input(student[]);
void print(student[]);
void initialize(student[]);

int main(){
	student enrolled[3];	//declaration of array of 3
	enrolled[0].firstName = "Jack";
	enrolled[0].lastName = " White";
	enrolled[0].ID = 433772;
	enrolled[0].testScores[0] = 89.87;
	enrolled[0].testScores[1] = 95.44;
	enrolled[0].testScores[2] = 87.56;
	enrolled[0].testScores[3] = 92.13;
	enrolled[0].testScores[4] = 96.53;

	print(enrolled);
	initialize(enrolled);
	print(enrolled);
	input(enrolled);
	print(enrolled);

	return 0;
}

void initialize(student s[])
{
	for(int i = 0; i < 3 ; i++)
	{
		s[i].firstName = "";
		s[i].lastName = "";
		s[i].ID = 0;
		for(int j = 0; j < 5; j++)
			s[i].testScores[j] = 0;
		
	}
}

void input(student s[]){
	for(int i = 0; i < 3; i++)
	{
		cout << "Enter firstname & lastname" << endl;
		cin >> s[i].firstName >> s[i].lastName;

		cout << "\nEnter student ID" << endl;
		cin >> s[i].ID;

		for(int j = 0; j < 5; j++)
		{
			cout <<"\nEnter Scores";
			cin >> s[i].testScores[j];
		}
	}
}

void print(student s[]){
	for(int i =0; i < 3;i++){
		cout << "Student Name: " << s[i].firstName << s[i].lastName << endl;
		cout << "Student ID: " << s[i].ID << endl;
		cout << "Test Scores:  ";
		for(int j = 0; j < 5; j++)
			cout << s[i].testScores << " ";
		cout << endl;
		s[i].GPA = (s[i].testScores[0] + s[i].testScores[1] + s[i].testScores[2] + s[i].testScores[3] + s[i].testScores[4]) / 5.0;
		cout << "Student GPA: " << s[i].GPA << endl << endl;
	}
}


on the testScores part, i get some weird values i dont know why
Line 74
 
            cout << s[i].testScores << " ";
you omitted the subscript j
 
            cout << s[i].testScores[j] << " ";

The output you get is the address rather than the contents of each element.
thanks bro!
You have an additional problem:

Lines 20-27: You initialize only the first entry.

Line 29: You call print, which prints the full array of 3 entries, but only the first entry has been initialized. You're going to print 2 garbage entries.


Last edited on
Topic archived. No new replies allowed.