In: Computer Science
I'm having a problem getting my code to function correct, *num_stu keeps losing its value when ever another function is called from the class. Can someone proof read this for me and let me know what'd up?
Header.h file
#include <iostream>
using namespace std;
class report {
private:
int* num_stu;
int xx;
int* id;
double* gpa;
public:
report(int x) {
num_stu = &x;
xx = x;
id = new int[*num_stu];
gpa = new double[*num_stu];
for (int i = 0; i < *num_stu; i++) {
id[i] = 0;
gpa[i] = 0;
}
}
void assign() {
cout <<"You still losing your value? " << *num_stu
<< endl << num_stu << xx << endl;;
for (int i = 0; i < *num_stu; i++) {
cout << "Enter ID: " << endl;
cin >> id[i];
cout << "Enter GPA: " << endl;
cin >> gpa[i];
}
}
void print_info()
{
for (int i = 0; i < *num_stu; i++) {
cout << id[i] << " " << gpa[i] << " ";
if (gpa[i] >= 3.8) {
cout << "an honor student. ";
}
cout << endl;
}
}
~report() {
delete[]id;
delete[]gpa;
}
};
Main.cpp file
#include <iostream>
#include "report.h"
using namespace std;
int main() {
report boy(5);
boy.assign();
boy.print_info();
}
class report { private: int *num_stu; int xx; int *id; double *gpa; public: report(int x) { num_stu = new int; // create a new integer *num_stu = x; // then use x to set value of num_stu xx = x; id = new int[*num_stu]; gpa = new double[*num_stu]; for (int i = 0; i < *num_stu; i++) { id[i] = 0; gpa[i] = 0; } } void assign() { cout << "You still losing your value? " << *num_stu << endl << num_stu << xx << endl;; for (int i = 0; i < *num_stu; i++) { cout << "Enter ID: " << endl; cin >> id[i]; cout << "Enter GPA: " << endl; cin >> gpa[i]; } } void print_info() { for (int i = 0; i < *num_stu; i++) { cout << id[i] << " " << gpa[i] << " "; if (gpa[i] >= 3.8) { cout << "an honor student. "; } cout << endl; } } ~report() { delete[]id; delete[]gpa; } };