Output

I am at my first running program implementing structs, I am stuck on syntax errors in printing my values, any help out there?

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

using namespace std;

struct studentType
{
	string first;
	string last;
	int score;
	char grade;
};

void getData(studentType[]);
char assignGrade(studentType[], int);
int indexHigh(const studentType[]);
void print(const studentType[]);

int main()
{
	studentType student[5];
	getData(student);
	print(student);
	return 0;
}

void getData(studentType list[])
{
	int index;
	cout<<"Enter name and score (last) (first) (score): "<<endl;
	for(index = 0; index < 5; index++)
	{
		cin>>list[index].last>>list[index].first>>list[index].score;
		list[index].grade = assignGrade(list, index);
	}
}

char assignGrade(studentType list[], int loc)
{
	char grade;
		if(list[loc].score >= 90)
		    grade = 'A';
		else if(list[loc].score >= 80)
		    grade = 'B';
		else if(list[loc].score >= 70)
		    grade = 'C';
		else if(list[loc].score >= 60)
		    grade = 'D';
	    else
		    grade = 'F';
	return grade;
}

int indexHigh(const studentType list[])
{
	int index, max = 0;
	for(index = 0; index < 5; index++)
		if(list[max].score > list[index].score)
			max = index;
	return max;
}

void print(const studentType list[])
{
	for(int index = 0; index < 5; index++)
		cout<<list[index].last<<", "<<list[index].first<<" score: "<<list[index].score<<", grade: "<<list[index].grade<<endl;
	cout<<"\nHigh test score(s):      "<<list[indexHigh(list)]<<endl;
}                                        // ^ ^ syntax error here, please help 
It looks like your indexHigh function is determining what the highest score is in the array, right? Then you return that value as in the subscript of the one in question.
Hmm, what syntax error are you getting?
Now I feel like an idiot, I just figured it out

 
list[indexHigh(list)].last<<endl;


Yeah, I couldn't figure out how to declare the function from within the array; the "." was throwing me off. My syntax error was saying:
no operator found which takes a right-hand operand of type 'const studentType' (or there is no acceptable conversion). I got it now, thanks tummy :)
what do you want to do with list[indexHigh(list)]
you can't print that because that's a struct..
1
2
3
4
5
ostream& operator << (ostream& printer, studentType data) {
    printer << data.first << " " << data.last << " "
    << data.score << " " << data.grade << endl;
    return printer;
}


you could add this code before your print() function
Hmmm...I think I see what you mean....I haven't been introduced classes or operatior overloading yet though and I'm trying to accomplish this per assignment requirements:
I do know that structs are a class in which all functions are public.
Your program MUST consist of the following:
A function to read the students' data into the array.
A function to assign the relevant grade to each student.
A function to find the highest test score.
A function to print the manes of the students having the high score.

Actually I have the syntax right now, my semantics need a little work as I keep printing the last persons' name entered, not the highest score for some reason..
Thank you for the input balckcoder41 :)
hey i'm also just learning c++ just like you..

this code
if(list[max].score > list[index].score)
should be
if(list[max].score < list[index].score)
lol...somehow I changed the greater than operator a few hours ago and it worked....I don't understand the logic at all....
1
2
if(list[max].score > list[index].score)
    max = index;


does this not mean if( the location of list, variable score, is greater than the location of list, variable socre)
max = stores the value of location;

I dont't get how the operator should be < but it IS semantically correct.
Explain please? I thought I learned how to manipulate arrays correctly :( now I'm confused again
1
2
3
4
5
6
7
8
9
10
11
12
int indexHigh(const studentType list[])
{
	int index, max = 0; //max=0; it assumes that index 0 has the
                        //highest value
	for(index = 0; index < 5; index++)
	//we check evey value in the array if the next
	//is higher then the value store in max is not
	//the highest so we change it this higher value
		if(list[max].score < list[index].score)
			max = index;
	return max;
}

it's like a boxing game, everyone challenges the champion, and if that challenger wins then his the new champion.. hope that makes sense..
Ah...I see what is happening, thank you blackcoder41. Don't know why I was trying to do it backwards lol.
Topic archived. No new replies allowed.