Delete more than one record from .txt file

So i get the concept of how to do this but i'm just not doing it right.
I have to ask the user if they want to delete any records. The user can enter -1 to finish deleting records. I have to write the remaining records in an output file.

SO i have this, but 1) it doesn't let me enter more than 1 id, and 2 it doesnt output anything to my output.txt.

records.txt
6
123 Jose Garcia 99
345 Luis Garcia 87
234 Jazlyn Soriano 79
456 Carter Sander 95
567 Natalia Soto 67
789 Isabel Santana 80
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
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;

class Student
{
private:
	int id;
	string firstName;
	string lastName;
	int examGrade;
public:
	Student();
	Student(int, string, string, int);
	void setall(int, string, string, int);
};

int main()
{
	fstream gradebook;
	fstream temp;
	gradebook.open("C://Users//Esmeralda//Desktop//Records.txt",ios::in);
	temp.open("C://Users//Esmeralda//Desktop//Output.txt",ios::out);
	if (gradebook.fail())
		{
			cout << "Error opening file" << endl;
			exit(0);
		}

	int size;
	gradebook >> size; 

	Student *records;
	records = new Student[size];
	int id;
	string firstName;
	string lastName;	
	int examGrade;
	int remove;
	string line;

	for (int i=0; i<size;i++)
	{
		gradebook >> id >> firstName >> lastName >> examGrade;
		records[i]=Student(id, firstName, lastName, examGrade);
		cout << id << "\t" <<  firstName << "\t" << lastName << "\t" <<  examGrade << endl;
	}
	
	cout << "Please enter the ID of the student to be deleted." << endl << "To exit, please type -1." << endl;
	cin >> remove;
	while (getline(gradebook,line))
		{
			if (remove == id)
			{
				temp << remove << endl;
				size--;			
			}
			else 
				exit(0);
		}
	gradebook.close();
	temp.close();

	delete [] records;
	system("pause");
	return 0;
}
Student::Student()
{
	id= 0;
	firstName=" ";
	lastName=" ";
	examGrade=0;
}
Student::Student(int theId, string theFirstName, string theLastName, int theGrade)
{
	setall(theId, theFirstName, theLastName, theGrade);
}
void Student::setall(int theId, string theFirstName, string theLastName, int theGrade)
{
	if(theId>999 || theId<100)
		exit(1);
	else
		id=theId;

	firstName = theFirstName;

	lastName = theLastName;

	if(theGrade>101 || theGrade<0)
		exit(1);
	else
		examGrade=theGrade;
}
Last edited on
1)

1
2
3
4
5
6
7
8
9
10
11
12
cout << "Please enter the ID of the student to be deleted." << endl << "To exit, please type -1." << endl;
cin >> remove;

while(getline(gradebook,line))
{
	if (remove == id)
	{
		temp << remove << endl;
		size--;			
	}
	else exit(0);
}


remove will never equal id; Here's the reason why:

Take for example remove = 123, for Jose Garcia.

getline(gradebook,line))

will retrieve

123 Jose Garcia 99


123 != "123 Jose Garcia 99"


2)

See 1, the if statement will never evaluate to true in order to print it to the text file.


EDIT:

Another thing:

 
remove == id


id = 789, the last id to be initialised by the for loop.
Last edited on
Ahh, i see.

But i cant put it == to Student(id, firstName, lastName, examGrade) because thats an array. How can i get it to equal a line?
Topic archived. No new replies allowed.