HELP PLEASE

This is a class exercise that I am doing. Everything goes fine without the delete[]pl at the end, but when I run the code with the delete, it gives me an error when i finally choose the option 3, exit. ¿How can I delete the dynamic array correctly? THANK YOU!
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
#include <iostream>
#include "Student.h"
using namespace std;

void main()
{
int n=0;
cout << "Dynamic objects"<<endl;
Student *pl[100]; //array of dynamic objects
pl[n] = new Student("Ana",6.0); //example how create a new Student
pl[n]->prt(); //ex. How print the data
n++;
int opc;
while(1){
	cout << "** Menu **\n1.Add 2.Prt 3.Exit? ";
	cin >> opc;
	if (opc==1){
		pl[n]=new Student;
		pl[n]->get();
		n++;
	}
	else if(opc==2){

		for(int i=0;i<n;i++){
			pl[i]->prt();
		}
	}
//Put your code here to Print
	else{
		break; //exits from while
	}
	
}
delete[]pl;
cout << "End" << endl;
}




THANK YOU VERY MUCH
Call delete on every pointer that you create using new.

Here is where you create pl:
Student *pl[100]; //array of dynamic objects
Was it created using new? No. So do not call delete on it.


You are creating every pointer that you store inside the array using new, so you must call delete on every pointer that you store inside the array.

How can I delete the dynamic array correctly?

There is no dynamic array here. Is there meant to be?

Last edited on
Ok Thank you. I have another question. This is my code, It's about students and their average mark. In option 3 (show nerds list), how can I acces the friend bool function passed correctly? THANK YOU VERY HELPFUL!!

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
#include <iostream>
#include "Student.h"
using namespace std;
void main(){
	int number;
	cout<<"Select number of students"<<endl;
	cin>>number;
int num=0;
Student *list=new Student[number];
int opc;
while(1){
	int opc;
	cout << "** Menu **\n1.Add data 2.See list 3.See nerds 4.Exit? ";
	cin>>opc;
	if(opc==1){
			for(int i=0;i<number;i++){

			list[i].get();
			}
			
	}
	else if(opc==2){
		for(int i=0;i<number;i++){
			list[i].prt();
		}
	}
	else if(opc==3){
		cout<<"NERDS LIST:\n";
		for(int i=0;i<number;i++){
			if (passed(list[i]==true){
				list[i].prt();
			}
		}
	}
	else{
		cout<<"Thank you"<<endl;
		exit(NULL);
	}
}


}


This is the definition of the friend bool function:
1
2
3
4
bool passed(Student *st){
if (st->average >= 5.0 ) return true;
else return false;
}


If I do the exercise this other way, it works, but I just want to knoe it is possible doing it the first way
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
#include <iostream>
#include "Student.h"
using namespace std;
void main(){
int num=0;
Student *list[100];
int opc;
while(1){
	int opc;
	cout << "** Menu **\n1.Add data 2.See list 3.See nerds 4.Exit? ";
	cin>>opc;
	if(opc==1){
			list[num]=new Student;
			list[num]->get();
			num++;
	}
	else if(opc==2){
		for(int i=0;i<num;i++){
			list[i]->prt();
		}
	}
	else if(opc==3){
		cout<<"NERDS LIST:\n";
		for(int i=0;i<num;i++){
			if (passed(list[i])==true){
				list[i]->prt();
			}
		}
	}
	else{
		cout<<"Thank you"<<endl;
		exit(NULL);
	}
}


delete[]list;
}

THANK YOU VERY MUCH

You've defined the function to take a Student*:
1
2
3
bool passed(Student *st){
//...
}


However, look at line 30:

if (passed(list[i]==true){

The thing you're passing in is list[i]==true. That expression evaluates to a bool, so you're trying to pass a bool into the function.

In fact, it also looks as though you're missing a close parenthesis there.

In future, if you have a problem with code that doesn't compile, then show us the error messages. The compiler gives error messages that provide useful information about what's wrong, so how could you possibly think it was a good idea to withold that information from us?

Ok, I put it this way if (passed(list[i])==true){
It doesn´t work anyway. The error messages I get are:
Error 2 error C2664: 'passed' : cannot convert parameter 1 from 'Student' to 'Student *'
3 IntelliSense: no suitable conversion function from "Student" to "Student *" exists

Warning 1 warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.

Can you please tell me how can I show the students that have passed (average>5) using the bool function? THANK YOU VERY MUCH
You've defined list to be an array of Student objects. So list[i] is a Student, not a Student*.
OK, I understand, but I dont know how to do this correctly. Can you please tell me how?
You have various options:

- you could pass the address of the array element into the function, rather than the element itself

- you could change the array so that it's an array of pointers, rather than an array of objects

- you could change the function, so that it takes an object rather than a pointer. Since the function doesn't modify the object, you don't need to worry about passing by reference.

It's up to you which one you choose.
Topic archived. No new replies allowed.