help please

Anyone know why this doesn't program doesn't work?

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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class Student
{
    
public:
    int iD;
    string firstName;
    string lastName;
    int examGrade;
    
    Student(){};
    ~Student(){};
   
    Student(int iD, string firstName, string lastName, int examGrade);
};
int main()
{
    // Variables
    Student Info;
    int option = 0;
    int size;
    int count;
    Student *records;
    
    fstream instream;
    instream.open("Records.txt");
    
    if(instream.fail())
    {
        cout << "File Output cannot be open. Try agin." << endl;
        exit(1);
    }
    instream >> size;
   
    records = new Student[size];
    for(int i=0; i<size;i++)
    {
        
        instream >> Info.iD;
        instream >> Info.firstName;
        instream >> Info.lastName;
        instream >> Info.examGrade;
        records[i] = Student(Info.iD, Info.firstName, Info.lastName, Info.examGrade);
    }
    instream.close();
    
    for(int i = 0; i< size; i++)
    {
        cout <<records[i].iD << " " << records[i].firstName << " " << records[i].lastName << " " << records[i].examGrade << endl;
    }
    count = size;
    
    while (option!=-1)
    {
        cout << "Please enter the ID of the student to be deleted. " << endl;
        cout << "To exit, please type -1. " << endl;
        cin >> option;
        
        for(int i = 0; i <size; i++)
        {
            // user erase the id that he/she wants
            if(option == records[i].iD)
            {
                records[i].iD = 0;
                count--;
            }
        }
    }
  
    fstream outstream;
    outstream.open("Output.txt",ios:: out);
    if(outstream.fail())
    {
        cout << "File Output cannot be open. Try agin." << endl;
        exit(1);
    }
    outstream << count <<endl;
    
    Student *temp;
    temp = new Student[size];
    int c = 0;
    for (int i = 0; i < size; i++)
    {
        
        if (records[i].iD != 0)
        {
            temp[c] = records[i];
            c++;
        }
    }
    
    delete [] records;
    records = new Student[count];
    
    for (int i = 0; i < count; i++)
    {
        records[i] = temp[i];
    }
    delete [] temp;
    for(int i = 0; i < count; i++)
    {
        outstream << records[i].iD << " " << records[i].firstName << " " << records[i].lastName << " " << records[i].examGrade << endl;
    }
    outstream.close();
    return 0;
   
}

Student::Student(int ID, string first_name, string last_name, int exam_grade)
{
    iD = ID;
    firstName = first_name;
    lastName = last_name;
    examGrade = exam_grade;
    
}
Last edited on
Please, describe the "doesn't work" with more details.
Topic archived. No new replies allowed.