Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C++
#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
C++
for (int i = 0; i < 5; i++)
		setScores(list[i]);

Also tried just
C++
setScores(list);

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

1 solution

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



Assuming Student.h contains something like
C++
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:
C++
int main()
{
  int score[] = {10,9,8,7,6}; 
  Student s(42, "Foo", "Bar", score);
  s.printStudent();
}
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900