Help with Vectors?

I'm trying to write a program where it can access a file with student ids and their names and be able to update, delete student records. I'm currently stuck on the deleting part as my current code is only deleting the whole record. And I cant figure out how to fix it.

I'm trying to get it into vectors, then input a last name, next the program would search the doc and delete the line with the last name then spit out a whole new file.

The format for the student file is as follows-

134, Doe, John

Here is what I have so far-

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
#include <string>
#include <iostream>
using namespace std;
#include "class.h"
#include <fstream>
#include <algorithm>
#include <vector>

void student:: remove(string person)
{
vector<string> file;
string temp;

ifstream infile("50student.txt");

while( !infile.eof() )
{
getline(infile, temp);
file.push_back(temp);
}
// done reading file
infile.close();

string item;

cout << "Enter a student to delete: ";
cin>>item;
getline(cin, item);

for(int i = 0; i < (int)file.size(); ++i)
{
if(file[i].substr(0, item.length()) == item)
{

file.erase(file.begin() + i);
cout << "Student erased!"<< endl;
i = 0; // Reset search
}
}

//write new order list back out
ofstream out("50student.txt", ios::out | ios::trunc);

for(vector<string>::const_iterator i = file.begin(); i != file.end(); ++i)
{
out << *i << endl;
}
out.close();
}


How its implemented
1
2
string ln;
r.remove(ln);
Topic archived. No new replies allowed.