HELP

have gotten this far, but somehow i keep getting an invalid error , please help. i am supposed to display the highest gpa score along with the name , but that is the part i cannot seem to get.

#include<iostream>
#include<string>
using namespace std;

class Student
{
private:
string name;
double gpa;
int rank;


public:
void setName(string nm)
{
name = nm;
}
void setGpa(double gp)
{
gpa = gp;
}
void setRank(int rnk)
{
rank = rnk;
}
string getName()
{
return name;
}
double getGpa()
{
return gpa;
}

int getRank()
{
return rank;
}
};





int main()
{
string firstName;
double gradePointAve, highestScore;
int studRank;
Student learner[3];

for (int x=0; x<3; x++)
{
cout << "First Name: ";
cin >> firstName;
cout << "Grade Point Average: ";
cin >> gradePointAve;
cout << endl;

learner[x].setName(firstName);
learner[x].setGpa(gradePointAve);
learner[x].setRank(studRank);

}

for (int x=0; x<2; x++)
{
if (learner[x].getGpa() > highestScore)
highestScore = x;

}

cout << "winner is:" << learner[highestScore].getName << "\n" << endl;


system("pause");
return 0;
}



closed account (1vRz3TCk)
Always initialise your variables.

highestScore is likely to have junk that is then never set to anything valid meaning learner[highestScore].getName will be out of scope...and getName is a function so learner[highestScore].getName()

(from reading the code, so I might have missed something)

PS. please use code tags
[code]
Your code
[/code]
Last edited on
Always pay attention to compiler errors and warnings.

uninitialized local variable 'studRank' used main.cpp	89	
expression must have integral or unscoped enum type main.cpp	100	
subscript is not of integral type	main.cpp	100	

Basically it means that you can't use a double as an array index.
To get the index of the best student use:
1
2
3
4
5
6
7
8
9
10
11
  highestScore = learner[0].getGpa();
  int highestIndex = 0;

  for (int x = 1; x < 3; x++)
  {
    if (learner[x].getGpa() > highestScore)
    {
      highestIndex = x;
    }
  }
  cout << "winner is:" << learner[highestIndex].getName() << "\n" << endl;

Also don't forget to read the student rank.
Thank you
Topic archived. No new replies allowed.