Problem with classes

The program is supposed to do the following. I want to use a class, named student, which consists of 3 variables, name, grade and height. The name should be alphabetical, in small letters, grade an integer raging from 0 to 100 and height a double variable raging from 1.20 to 2.20. Then, output the final correct inputs in a board-like way. I've made the class, but the program crashes immediately after outputting the data. I don't why it's doing this. Also some advice on how to make a destructor correctly would be appreciated.

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
  #include<iostream>
#include<windows.h>
#include<string>
#include<vector>
using namespace std;
bool is_name(string id){
int a = id.size();
bool dis = true;
for (int i=0; i<a; i++){
    if (id[i]<'a' || id[i]>'z'){
        dis = false;
    }
}
return dis;
}

class Student{
public:
    string name;
    unsigned int grade;
    double height;
public:
    void make_student(string n, unsigned int g, double h){
    name = n;
    grade = g;
    height = h;
    }
    void delete_every (){
    delete &name;
    delete &grade;
    delete &height;
    }
    Student(){
    name = " ";
    grade = 0;
    height = 0.000000;
    }
    ~Student(){
    if (is_name(name)==false){
    delete_every();
    }
    else if (grade>100){
        delete_every();
    }
    else if(height<1.20 || height<2.20){
        delete_every();
    }
    }
};

int main(){
vector <Student> school;
int n;
cin>>n;
school.assign(n, Student());
string in_name;
unsigned int in_grade;
double in_height;
for (int i=0; i<n; i++){
    cin>>in_name;
    cin>>in_grade;
    cin>>in_height;
    school[i].make_student(in_name, in_grade, in_height);
}
cout<<"Names:       ";
cout<<"Grades:      ";
cout<<"Height:      \n";

for (int i=0; i<n; i++){
    cout<<school[i].name<<"         "<<school[i].grade<<"           "<<school[i].height<<"\n";
}
return 0;
}


PS
I am completely self taught, so please be a bit thorough on your explanations, thanks
learn to indent.

You ought to only delete what you new
There is no need for a destructor.
I didn't use new anywhere. Could you elaborate?
Use delete only on something that you created using new.
It looks like what you're trying to do is to remove a student with invalid data. I think you should approach this in a different way. Don't create a student at all until you have valid data first. Also, your make_student function is fulfilling a role that should be played by a constructor.
Your current student class would then be reduced to this:
1
2
3
4
5
6
7
8
9
10
class Student
{
public:
    string name;
    unsigned int grade;
    double height;
public:
    Student() : name(" "), grade(0), height(0.0) { }
    Student(string n, unsigned int g, double h) : name(n), grade(g), height(h) { }
};
If you aren't familiar with that constructor syntax, read this section of the tutorial: http://www.cplusplus.com/doc/tutorial/classes/#member_initialization
Last edited on
Thanks everyone!
Topic archived. No new replies allowed.