Following is my code,
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Student {
private:
string name;
string roll;
public:
Student() {}
void input()
{
cout << "Enter Name: ";
getline(cin, name);
cout << "Enter Roll Number: ";
getline(cin, roll);
savestudent();
}
void savestudent()
{
ofstream WriteStudent;
WriteStudent.open("Student.FILE", ios::in | ios::app);
WriteStudent << name << "\n";
WriteStudent << roll << "\n";
WriteStudent.close();
cout << "Success! Data has been saved!" << endl;
}
void print()
{
cout << "Name: " << name << endl
<< "Roll Number: " << roll << endl;
}
void setname(string a)
{
name = a;
}
void setroll(string a) { roll = a; }
};
Student **p = NULL;
int studentsize = 0;
void resize();
void LoadStudent();
int main()
{
bool fh = true;
while (fh)
{
cout << "Pick your option" << endl
<< "1) Add a New Student" << endl
<< "2) Display The Students" << endl
<< "3) Quit the Program " << endl
<< "Enter a Number: ";
int choice;
cin >> choice;
cin.ignore();
switch (choice)
{
case 1:
resize();
p[studentsize - 1] = new Student;
p[studentsize - 1]->input();
break;
case 2:
LoadStudent();
for (int i = 0; i < studentsize; i++)
p[i]->print();
break;
case 3:
fh = false;
break;
default:
fh = false;
}
}
cout << endl;
system("pause");
return 0;
}
void resize()
{
Student **temp = new Student*[studentsize + 1];
for (int i = 0; i < studentsize; i++)
temp[i] = p[i];
delete[]p;
p = temp;
studentsize++;
}
void LoadStudent()
{
int a = 0;
ifstream LoadStuff;
LoadStuff.open("Student.FILE", ios::in);
delete[]p;
studentsize = 0;
string temp;
while (getline(LoadStuff, temp))
{
resize();
p[studentsize - 1] = new Student;
p[studentsize - 1]->setname(temp);
getline(LoadStuff, temp);
p[studentsize - 1]->setroll(temp);
}
}
|
What I did here is make a simple Student class with two data members of string datatype.
Then I have made a double pointer which will point to an array of single pointers... This array will be resized on every new entry of student the user asks for.
I have made a function to write this data into a file (which is working fine).
I have also made a function which will read this data from a file into the same double pointer array and then display the data (this also works fine if you select this option first)
So, as a test program, I wrote this data into the file and then closed the program,
1 2 3 4 5 6 7
|
None
12F-0128
Ashas
12F-014444
Bool
12F-23232
|
Then, I ran the program again and this time, instead of adding a new student, I went for display data and the program read the data from the file and displayed it on console - this worked perfectly again.
However, when I tried to display this data now, it gives runtime error in the resize function (which is as follows,)
1 2 3 4 5 6 7 8 9 10 11
|
void resize()
{
Student **temp = new Student*[studentsize + 1];
for (int i = 0; i < studentsize; i++)
temp[i] = p[i];
for (int i = 0; i < studentsize; i++)
delete[i]p;
delete[]p;
p = temp;
studentsize++;
}
|
Even if I delete the for loop in it, it still gives error on "delete[]p;"
However, If remove both of these lines, and make the resize function as follows,
1 2 3 4 5 6 7 8 9
|
void resize()
{
Student **temp = new Student*[studentsize + 1];
for (int i = 0; i < studentsize; i++)
temp[i] = p[i];
p = temp;
studentsize++;
}
|
Then the code works.
Now, I don't get as to why this deletion of pointer array is not working.. As far as my logic is concerned, I think the code should be working. Can anyone explain this, please? Thank you.
PLEASE NOTE: WE HAVEN'T LEARNED VECTORS YET AND THAT'S WHY WE CAN'T USE THEM. Hence, I must make this resize function for my code to properly work