Calculate average and use multiple int

I want to create a new project in C++, but i have a little problem.
My project is about university admission, and for every student i need this informations: name, birth date, high school final exam note, and than they have to take 2 exams(each of these 2 exams have 3 notes) and i have to make an average for admission note(like 25% high school final note and 75% from the 2 exams).

I must add average note and high school final note in constructor? And can i use multiple integers into a function?

I hope you understand what i want to do.
I assume "note" means "grade".

The 2 exams have 3 grades each? What does that mean?

I must add average grade and high school final grade in constructor?

What are you asking here?

And can i use multiple integers into a function?

Yes you can pass multiple integers (or whatever) to a function.
Each exam is examined by 3 teachers, each teacher gives a grade, and the average of these three grades is the final grade for that exam.

My question is how can i create that average function on the last lane of my code ?
Average must be made like this: grade_bac*0.25 (that's the high school final exam baccalaureate grade) and the averege of the two exams.

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <string>

class Student {
private:
    std::string name;
    int birth_day;
    int birth_month;
    int birth_year;
    int grade_bac;
    int grade1_ex1;
    int grade2_ex1;
    int grade3_ex1;
    int grade1_ex2;
    int grade2_ex2;
    int grade3_ex2;
    int average;

public:
    Student(std::string name, int birth_day, int birth_month, int birth_year, int grade_bac, int grade1_ex1,int grade2_ex1,
            int grade3_ex1, int grade1_ex2, int grade2_ex2, int grade3_ex2, int average);
    Student(const Student &source);
    ~Student();

    void set_name(std::string name)
    {
        this->name = name;
    }

    std::string get_name() const
    {
        return name;
    }

    void set_birth_day(int birth_day)
    {
        this->birth_day = birth_day;
    }

    int get_birth_day() const
    {
        return birth_day;
    }

    void set_birth_month(int birth_month)
    {
        this->birth_month = birth_month;
    }

    int get_birth_month() const
    {
        return birth_month;
    }

    void set_birth_year(int birth_year)
    {
        this->birth_year = birth_year;
    }

    int get_birth_year() const
    {
        return birth_year;
    }

    void set_grade_bac(int grade_bac)
    {
        this->grade_bac = grade_bac;
    }

    int get_grade_bac() const
    {
        return grade_bac;
    }

    void set_grade1_ex1(int grade1_ex1)
    {
        this->grade1_ex1 = grade1_ex1;
    }

    int get_grade1_ex1() const
    {
        return grade1_ex1;
    }

    void set_grade2_ex1(int grade2_ex1)
    {
        this->grade2_ex1 = grade2_ex1;
    }

    int get_grade2_ex1() const
    {
        return grade2_ex1;
    }

    void set_grade3_ex1(int grade3_ex1)
    {
        this->grade3_ex1 = grade3_ex1;
    }

    int get_grade3_ex1() const
    {
        return grade3_ex1;
    }

    void set_grade1_ex2(int grade1_ex2)
    {
        this->grade1_ex2 = grade1_ex2;
    }

    int get_grade1_ex2() const
    {
        return grade1_ex2;
    }

    void set_grade2_ex2(int grade2_ex2)
    {
        this->grade2_ex2 = grade2_ex2;
    }

    int get_grade2_ex2() const
    {
        return grade2_ex2;
    }

    void set_grade3_ex2(int grade3_ex2)
    {
        this->grade3_ex2 = grade3_ex2;
    }

    int get_grade3_ex2() const
    {
        return grade3_ex2;
    }

    int average


};
Last edited on
I guess it would be somethinglike this:

1
2
3
4
5
6
7
int sum1 = grade1_ex1 + grade2_ex1 + grade3_ex1;
double avg1 = sum1 / 3.0;

int sum2 = grade1_ex2 + grade2_ex2 + grade3_ex2
double avg2 = sum2 / 3.0;

double avg = 0.25 * grade_bac + 0.75 * ((avg1 + avg2) / 2.0);

BTW, I doubt you need a copy ctor and a dtor.

And you could use an array to hold the grades, like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const int NumExams = 2, NumGradesPerExam = 3;
//...

    int grades[NumExams][NumGradesPerExam];
    //...

    double sum_of_avgs = 0.0;
    for (int e = 0; e < NumExams; ++e)
    {
        int sum = 0;
        for (int g = 0; g < NumGradesPerExam; ++g)
            sum += grades[e][g];
        sum_of_avgs += double(sum) / NumGradesPerExam;
    }

    double avg = 0.25 * grade_bac + 0.75 * (sum_of_avgs / NumExams);

Thank you for help, but now i have a little problem when i implemented a function to add students into a vector.

I have this Students.h and Students.cpp

Code for Students.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <vector>
#include <string>

class Students {
private:
    std::vector<Student> vect;

public:
    Students();
    ~Students();

    bool add_student(std::string name, int birth_day, int birth_month, int birth_year, double grade_bac, double grade1_ex1, double grade2_ex1,
                     double grade3_ex1, double grade1_ex2, double grade2_ex2, double grade3_ex2);

    void display() const;
};
 


And for Students.cpp is:
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
 #include "Students.h"
#include <string>
Students::Students(){

}

Students::~Students() {

}

bool Students::add_student(std::string name, int birth_day, int birth_month, int birth_year, double grade_bac,
        double grade1_ex1, double grade2_ex1, double grade3_ex1, double grade1_ex2, double grade2_ex2, double grade3_ex2)
{
    for(const Student &student: vect){
        if(student.get_name() == name){
            return false;
        } else {
            Student temp (std::string name, int birth_day, int birth_month, int birth_year, double grade_bac, double grade1_ex1, double grade2_ex1,
                          double grade3_ex1, double grade1_ex2, double grade2_ex2, double grade3_ex2);
            vect.push_back(temp);
            return true;

        }
    }

}



The error is get are:
1
2
3
In member function 'bool Students::add_student(std::__cxx11::string, int, int, int, double, double, double, double, double, double, double)':
D:\Projects\FacultacyAdmission\Students.cpp:24:32: error: no matching function for call to 'std::vector<Student>::push_back(Student (&)(std::__cxx11::string, int, int, int, double, double, double, double, double, double, double))'
             vect.push_back(temp);


1
2
note: candidate: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = Student; _Alloc = std::allocator<Student>; std::vector<_Tp, _Alloc>::value_type = Student]
       push_back(const value_type& __x)


note: no known conversion for argument 1 from 'Student(std::__cxx11::string, int, int, int, double, double, double, double, double, double, double) {aka Student(std::__cxx11::basic_string<char>, int, int, int, double, double, double, double, double, double, double)}' to 'const value_type& {aka const Student&}'

note: candidate: void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = Student; _Alloc = std::allocator<Student>; std::vector<_Tp, _Alloc>::value_type = Student]
Last edited on
Remove average from the constructor of Student. That should be computed.

In Students.cpp, line 18 declares a function called temp() that returns a student. To construct an object. To call the constructor, don't specify the types of the arguments:
1
2
3
            Student temp (name, birth_day, birth_month, birth_year,
                          grade_bac, grade1_ex1, grade2_ex1,
                          grade3_ex1, grade1_ex2, grade2_ex2, grade3_ex2);


In add_student(), you're returning from the function in both the if and the else branches. So you always return on the first iteration of the loop.

It looks like you're trying to add a student, but only if they aren't already there. Move the code that adds the student to after the loop:
1
2
3
4
5
6
7
8
9
10
11
12
    for(const Student &student: vect){
        if(student.get_name() == name){
            return false;
        }
    }

    // If you get here then the student wasn't found. Insert it.
    Student temp (name, birth_day, birth_month, birth_year,
                  grade_bac, grade1_ex1, grade2_ex1,
                  grade3_ex1, grade1_ex2, grade2_ex2, grade3_ex2);
    vect.push_back(temp);
    return true;


Personally, I'd change add_student to pass a Student object as the parameter instead of the 11 parameters to the constructor:
1
2
3
4
5
6
7
8
9
10
11
12
bool Students::add_student(const Student &stu)
{
    for(const Student &student: vect){
        if(student.get_name() == stu.get_name()){
            return false;
        }
    }

    // If you get here then the student wasn't found. Insert it.
    vect.push_back(stu);
    return true;
}

@dhayden Thank you so much for your help.
Now i am almost done with this mini-project, but when i add a function to Students.cpp called display(), i get one error that i can't understand...i tried without the const, in for loop i tried to modify my loop but i get the same error.

Can you please tell me when and why this error occurs?

1
2
3
In member function 'void Students::display() const':
D:\...\Students.cpp:39:42: error: expected primary-expression before ')' token
         for(const auto &Student: Students)


The function is:
1
2
3
4
5
6
7
8
9
10
11
void Students::display() const
{
    if(vect.size() == 0){
        std::cout << "Error: There are no students!\n" << "Please add one student or more!" << std::endl;
    } else {
        std::cout << "====================================================" << std::endl;
        for(const auto &Student: Students)
            Student.display();
        std::cout << "===================================" << std::endl;
    }
} 


And in main.c i have also some errors.

This is my main.c code:
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
#include <iostream>
#include "Students.h"

bool Students::add_student(std::string name, int birth_day, int birth_month, int birth_year, double grade_bac,
                           double grade1_ex1, double grade2_ex1, double grade3_ex1, double grade1_ex2,
                           double grade2_ex2, double grade3_ex2);

bool Students::add_student(std::string name, int birth_day, int birth_month, int birth_year, double grade_bac,
                           double grade1_ex1, double grade2_ex1, double grade3_ex1, double grade1_ex2,
                           double grade2_ex2, double grade3_ex2)
{
    if(vect.add_student(name, birth_day, birth_month, birth_year, grade_bac, grade1_ex1, grade2_ex1, grade3_ex1,
            grade1_ex2, grade2_ex2, grade3_ex2)){
        std::cout << name << " was added!" << std::endl;
    } else {
        std::cout << name << " already exists!" << std::endl;
    }

}
int main() {
    Students my_stud;
    my_stud.display();

    add_student("mark", 20, 04, 1998, 9.50, 8.75, 8.95, 9.00, 7.50, 6.30, 7.00);
    return 0;
}


The errors are:
1
2
error: declaration of 'bool Students::add_student(std::__cxx11::string, int, int, int, double, double, double, double, double, double, double)' outside of class is not definition [-fpermissive]
                            double grade2_ex2, double grade3_ex2); 


1
2
3
In member function 'bool Students::add_student(std::__cxx11::string, int, int, int, double, double, double, double, double, double, double)':
D:\...\main.cpp:12:13: error: 'class std::vector<Student>' has no member named 'add_student'
     if(vect.add_student(name, birth_day, birth_month, birth_year, grade_bac, grade1_ex1, grade2_ex1, grade3_ex1, 


1
2
3
In function 'int main()':
D:\...\main.cpp:24:79: error: 'add_student' was not declared in this scope
     add_student("mark", 20, 04, 1998, 9.50, 8.75, 8.95, 9.00, 7.50, 6.30, 7.00);


If you want, you can download the files of my mini-project from here, to test it out and to be easier for you to see the erros: https://drive.google.com/open?id=1GPF87C9DNqB0yM8osnULZKMqAo4AK0wo
Last edited on
main.cpp compiles when it's this:
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
#include <iostream>
#include "Students.h"

bool Students::add_student(std::string name, int birth_day, int birth_month, int birth_\
year, double grade_bac,
                           double grade1_ex1, double grade2_ex1, double grade3_ex1, dou\
ble grade1_ex2,
                           double grade2_ex2, double grade3_ex2)
{
    Student stu(name, birth_day, birth_month, birth_year, grade_bac,
                grade1_ex1, grade2_ex1, grade3_ex1,
                grade1_ex2, grade2_ex2, grade3_ex2);
    vect.push_back(stu);
    return true;
}

If you want add_student() to look for an existing record for the same student then you'll have to add code to do that.

int main() {
    Students my_stud;
    my_stud.display();

    my_stud.add_student("mark", 20, 04, 1998, 9.50, 8.75, 8.95, 9.00, 7.50, 6.30, 7.00)\
;
    return 0;
} 

Topic archived. No new replies allowed.