Click here to Skip to main content
14,218,541 members
Rate this:
Please Sign up or sign in to vote.
I am having trouble figuring out the syntax for how i am suppose to Call the member function to copy the values of my array into the appropriate member variables. This is specifically for the overloaded constructor because I think all the other functions are correct but they might not be.

Student.cpp
#include <iostream>
#include <fstream>
#include <string>
#include "Student.h"
using namespace std;

void Student::setID(int tID)
{
	ID = tID;
}

void Student::setFName(string f)
{
	firstName = f;
}

void Student::setLName(string l)
{
	lastName = l;
}

void Student::setScores(int list[])
{
	for (int i = 0; i < 5; i++)
		scores[i] = list[i];
	//scores[5] = list[5];
}

int Student::getID()
{
	return ID;
}

string Student::getFName()
{
	return firstName;
}

string Student::getLName()
{
	return lastName;
}

int Student::getWeightedTotal()
{
	int WeightedTotal = 0;
	WeightedTotal = 25 % (scores[0]) + 25 % (scores[1]) + 30 % (scores[2]) + 10 % (scores[3]) + 10 % (scores[4]);
	return WeightedTotal;
}

char Student::getGrade() // should be working 
{
	char grade = 0;
	if (getWeightedTotal() >= 90)
		grade = 'A';
	else if (getWeightedTotal() >= 80)
		grade = 'B';
	else if (getWeightedTotal() >= 70)
		grade = 'C';
	else if (getWeightedTotal() >= 60)
		grade = 'D';
	else
		grade = 'F';
	return grade;
}

void Student::printStudent()
{
	cout << "Student ID" << "    " << "Full Name" << "         " << "Scores" << "     " << "Weighted Total" << "     " << "Grade" << endl;
	cout << ID << "       " << firstName << " " << lastName << " " <<
		scores[0] << scores[1] << scores[2] << scores[3] << scores[4] << " " << getWeightedTotal() << "   " << getGrade() << endl;
}

Student::Student()
{
	ID = 0;
	firstName = "";
	lastName = "";

	for (int i = 0; i < 5; i++) // for loop to set each element to 0
		scores[i] = 0;


	//scores[5] = { 0 };

}

Student::Student(int tID, string f, string l, int list[])
{
	setID(tID);
	setFName(f);
	setLName(l);
	for (int i = 0; i < 5; i++)
		setScores(list[i]);
	//setScores(list);


}

The problem is right above this text in the overloaded constructor and i'm not sure what to do

What I have tried:

I've tried
for (int i = 0; i < 5; i++)
		setScores(list[i]);

Also tried just
setScores(list);

also tried
for (int i = 0; i < 5; i++)
		scores[i] = list[i];
Posted
Updated 6-Mar-17 10:41am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

The correct syntax is
Student::Student(int tID, string f, string l, int list[])
{
  setID(tID);
  setFName(f);
  setLName(l);
  setScores(list);
}



Assuming Student.h contains something like
class Student
{
  int ID;
  int scores[5];
  string firstName, lastName;
public:

  void setID(int tID);
  void setFName(string f);
  void setLName(string l);
  void setScores(int list[]);
  int getID();
  string getFName();
  string getLName();
  int getWeightedTotal();
  char getGrade(); // should be working 
  void printStudent();
  Student();
  Student(int tID, string f, string l, int list[]);

};


The code compiles and runs correctly with the following main:
int main()
{
  int score[] = {10,9,8,7,6}; 
  Student s(42, "Foo", "Bar", score);
  s.printStudent();
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100