Llor1s
Llor1s

Reputation: 59

How to print Student's grades (class Student) in C++

I have a class Student with

string surname; unsigned int number_of_gradebook; static int size; unsigned int *p = new unsigned int[size];

But an array of grades seems not to be outputed.

My code:

#include <iostream>
#include <conio.h>
#include <string>
using namespace std;

class Student {
private:
    string surname;
    unsigned int number_of_gradebook;
    static int size;
    unsigned int *p = new unsigned int[size];
public:
    Student(string s, unsigned int n, static int size, unsigned int *q):surname(s) {
        number_of_gradebook = n;
        //unsigned int *p = new unsigned int[size];
        for (int i = 0; i++; i < size) {
            p[i]=q[i];
        }
    }
    Student() {}
    ~Student() { delete[]p; }

    //friend std::ostream& operator<<(std::ostream&, const Student);
    string GetSurname() const { return surname; }
    unsigned int GetNumOfGrdb() const { return number_of_gradebook;  }
    unsigned int GetMarks() const { return *p; }
    void Print_Student();

};

int Student::size=0;

void Student :: Print_Student() {
    cout << "Student: " << surname << "\t" << number_of_gradebook << endl;
    if (size) {
        for (int i = 0; i++; i < size) {
            cout << p[i] << "\t";
        }
    }
}

class Group {
    Student *s;
    int count;
public:
    Group(int n=0) {
        s = new Student[count = n];
    }
    ~Group() { delete[]s; }
    friend void AddStudent();
    friend void DeleteStudent();
    friend void FindAveragePoint();
    friend void PrintFiveBest();
    void output() {
        cout << s << endl;
    }
};

int main() {
    Student A("Ivanov", 45368400, 5, new unsigned int[5] {3,4,4,5,3});
    A.Print_Student();
    cout << A.GetMarks();
    system("pause");
}

Any suggestions?

Upvotes: 0

Views: 1112

Answers (2)

Nikhil Badyal
Nikhil Badyal

Reputation: 1697

I have modified a bit of your code. But let me tell you your mistakes.

  1. You are unnecessarily writing static in a parameter of the constructor.
  2. Learn the syntax of for loop. it's for(init , cond,increment){}
  3. In your print value of size is zero. So if the condition is false it will never execute code. See this and let me know if you didn't get any part.
class Student {
private:
    string surname;
    int number_of_gradebook;
    int size;
    int *p;
public:
    Student(string s,int n,int siz, int *q):surname(s) {
        size = siz;
        number_of_gradebook = n;
        p = new int[siz];
        for (int i = 0; i < size;i++) {
            p[i]= q[i];

        }
    }
    Student() {}
    void GetMarks() const {
        for(int i=0;i<size;++i)
        {
            cout<<p[i]<<endl;
        }
    }
    void Print_Student();
};
void Student :: Print_Student() {
    cout << "Student: " << surname << "\t" << number_of_gradebook << endl;
    for(int i=0;i<size;++i)
    {
        cout<<p[i]<<endl;
    }
}
int main() {
    int arr[] = {1,2,3,4,5};
    Student A("Ivanov", 69, 5, arr);
    A.Print_Student();
     A.GetMarks();
   system("pause");
}


Upvotes: 1

Lukas-T
Lukas-T

Reputation: 11350

Assuming that this is some assignment and you are not allowed to use STL. If you are allowed to use STL, than throw out all the manual memory management and use std::vector instead. If not, read on.

The member size is always 0. And there is a problem with the design of size. If it is static (the same for all instances of Student) then why is there a parameter size in the constructor that does nothing? I think size should be a normal member and not static.

Student(string s, unsigned int n, int size, unsigned int *q) : 
    surname(s),
    number_of_gradebook(n),
    size(size),
    p(new unsigned int[size])
{
    ...
}


The other problem, is this messed up loop: for (int i = 0; i++; i < size)
Should be for (int i = 0; i < size; i++)

Be careful, you repeat this error further down in your code.


Also this line: s = new Student[count = n]; may work, but it does not do, what you might think it does, is quiet hard to read and should be broken in two lines, or even better, in the initializer list:

Group(int n) : 
    count(n),
    s(new Student[count])
{...}

Keep in mind, that new X[0] is not valid. So better you remove the default value or use something else than 0! Or even better: use vectors.

Upvotes: 1

Related Questions