String to const char error

Hello, I'm currently finishing up an assignment that was half written by my professor. Below in the testGrades section of code there are two errors both are the same message. Error: no matching function for call to Grades:: Grades(const char [15])
Test Grades
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

//Course: 4002-210
//Author: Rayno Niemi
//Lab 4 Exercise 1
//Name: testGradeEx1.cpp
//Purpose: Test program for the class Grades
//  Create stu1 Grades object
//  Add 5 grades to stu1 - only 3 can be stored in stu1 - other 2 discarded
//  Create stu2 Grades object
//  Add only 2 grades

#include <iostream>
#include <string>
#include <iomanip>

#include "Grades.h"

using namespace std;

int main ()
{
    //extra set of braces added so that value of destructor will be printed
    {

        cout << endl;
        //create Grades object to hold 4 grades and print it.

        Grades stu1("Tom Terrific");
        stu1.printGrades();

        //add 3 grades for Tom T
        stu1.addGrade(77.7);
        cout << endl;
        stu1.printGrades();

        stu1.addGrade(88.8);
        cout << endl;
        stu1.printGrades();

        stu1.addGrade(99.9);
        cout << endl;
        stu1.printGrades();

        //attempt to add two more grades - no space to store grades
        stu1.addGrade(100.0);
        cout << endl;
        cout << "Attempt to add grade of 100.0 - not stored" << endl;
        stu1.printGrades();

        stu1.addGrade(90.0);
        cout << endl;
        cout << "Attempt to add grade of 90.0 - not stored" << endl;
        stu1.printGrades();

        //add 2 grades for Mary M
        cout << endl << endl;
        Grades stu2("Mary Marvelous");

        stu2.addGrade(92.5);
        stu2.addGrade(98.0);
        stu2.printGrades();


        //compute and print averages
        cout << setprecision(2) << showpoint << fixed;
        cout << endl;
        cout << "Average for " << stu1.getName() << " is " << stu1.calcAverage( ) << endl;
        cout << "Average for " << stu2.getName() << " is " << stu2.calcAverage( ) << endl;
        cout << endl;
    }//Grades objects now destructed at end of braces in which created

    cout << endl;
    return 0;

} //end main


Grades.h
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



#ifndef GRADES_H_INCLUDED
#define GRADES_H_INCLUDED

#include <iostream>
#include <string>
using namespace std;
class Grades{


public:

    Grades();
    Grades(string name, int maxGrades, int numGrades, double gradeList[]);
    ~Grades();
    double calcAverage();
    bool addGrade(double);
    string    getName();
    void    printGrades();






private:
int maxGrades;
int numGrades;
double gradeList[3];
string name;
};
#endif // GRADES_H_INCLUDED 


Grades.cpp
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


#include <iostream>
#include <string>
#include <iomanip>
#include "Grades.h"

Grades :: Grades()
{
    int maxGrades = 3;
    int numGrades = 0;
    double gradeList[] = {-1 , -1 , -1};
    string name;
}

Grades::Grades(string name, int maxGrades, int numGrades, double gradeList[]){

    this -> name = name;
    this -> maxGrades = maxGrades;
    this -> numGrades = numGrades;
    this -> gradeList[3] = gradeList[3];

};

Grades :: ~Grades(){
    cout << " deconstructor" << endl;

}

void Grades :: printGrades(){

    for (int i = 0; i <= 3; i++ )
    {
        cout << gradeList[i] << endl;
    }
};

double Grades :: calcAverage() {

    for (int i = 0; i <= 3; i++)
    {
       gradeList[i] = gradeList[i] + gradeList[i];

    }
    cout << " this is gradeList " << gradeList[1];

};

bool Grades :: addGrade(double newGrade){

    if(gradeList[3] < 3)
    {
        gradeList[3]++;
        return true;
    }
    else
    return false;
};

string Grades :: getName (){
    cin >> name;
    return name;
};

Constructors defined:
1
2
    Grades();
    Grades(string name, int maxGrades, int numGrades, double gradeList[]);


Attempted constructions:
1
2
        Grades stu1("Tom Terrific");
        Grades stu2("Mary Marvelous");


Do you notice any differences between what you've defined and what you're trying to use?
In other words: Add another constructor that takes just the name and maxGrades (you may use a default value).

In Grades.h on line 31: gradeList must not have a fixed size. Allocate the requried size according to maxGrades in your constructor.

In Grades.cpp: addGrade() is entirely wrong. You need to check numGrades against maxGrades. Store newGrade according to numGrades and then increase numGrades
The addGrade & the calcAverage method I'm currently confused about. I fixed the other two problems as specified by your comments thank you both. Can someone please help me with the addGrade & the calcAverage methods?

calcAverage
This is supposed to calculate the average of the 3 grades in the array.
1
2
3
4
5
6
7
8
9
10
double Grades :: calcAverage() {

    for (int i = 0; i <= 3; i++)
    {
       gradeList[i] = gradeList[i] + gradeList[i];

    }
    cout << " this is gradeList " << gradeList[1];

};


addGrade
This is supposed to add the grade if the array isn't full (less than 3 elements).
1
2
3
4
5
6
7
8
9
10
bool Grades :: addGrade(double newGrade){

    if(numGrades < maxGrades)
    {
        numGrades = gradeList[0]++;
        return true;
    }
    else
    return false;
};
calcAverage
This is supposed to calculate the average of the 3 grades in the array.


How does one calculate an average? You take the sum of all elements in the array and divide it by the number of elements. Where do you do that? Why aren't you returning a value like the function signature says you're going to do? Does it make sense to modify the array as you are doing? If it doesn't make sense to modify the array, why is this function not const?

for (int i = 0; i <= 3; i++)

If there are 3 elements in the array and you're using indices 0, 1, 2 and 3... how many elements are you accessing?


addGrade
This is supposed to add the grade if the array isn't full (less than 3 elements).

Given that numGrades is number of grades currently in the array, what index should you add the next element to? Does it make sense to increment an element of the array as you do? Does it make sense to assign numGrades the value of one of the grades in the list?

Last edited on
for addGrade(): You need to assign newGrade. This way it does not make sense. You certainly need to wrap your mind around the programming logic.

1
2
3
4
5
6
7
8
9
10
11
bool Grades :: addGrade(double newGrade){

    if(numGrades < maxGrades)
    {
        gradeList[numGrades] = newGrade; // assign newGrade accordingg to numGrades
        numGrades++; // now increase numGrades because there's one more element
        return true;
    }
    else
    return false;
};
Topic archived. No new replies allowed.