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!
#include <iostream>
#include "Student.h"
usingnamespace 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++;
}
elseif(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;
}
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!!
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 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.