I am trying to remove a line containing username and password from text file. 
so I tried to following,

1)Read the text file.

2)store the userName and password inside the textfile.

3)prompt the user which user he wants to delete.

4)the entire line will be deleted in the vector if the input matches with the username in the vector. 

5)copy back to text file(not done yet)

I managed to get it by luck
 **textfile(userInfo.txt)**

```
amber abc
janet def
chris DEF
gerald AZC

```

my output of my codes

```
Before Deletion
amber abc
janet def
chris DEF
gerald AZC

Enter User Name to delete: amber

After Deletion
janet def
chris DEF
gerald AZC

```

but what I don't really understand is, how my codes actually delete the entire line after it matches with my input espcially starting from

```
    string name;
    cout << "Enter User Name to delete: ";
    cin >> name;
    cout << " " << endl;
    cout << "After Deletion" << endl;
    for (int i =0; i<userDetails.size(); i++) {
       if(userDetails[i].getUserName() == name){
            userDetails.erase(userDetails.begin() + i);
       }
        cout << userDetails[i].getUserName() << " " << userDetails[i].getPassword() << "\n";
    } 

```

I hope someone can explain it to me.

my codes

**user.h**

```
#ifndef user_user_h
#define user_user_h
#include <iostream>

class user  {

public:
   user() {
        userName = " ";
        password = " ";
   }

    user(std::string userName,std::string password);

    std::string getUserName();
    std::string getPassword();

    void setUserName(std::string userName);
    void setPassword(std::string password);

private:
    std::string userName,password; 
};
#endif

```

**main.cpp**

```
#include "user.h"
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>

using namespace std;

user::user(string userName,string password)  {
    setUserName(userName);
    setPassword(password);
};

string user::getUserName()    {
    return userName;
}
string user::getPassword()  {
    return password;
}

void user::setUserName(std::string userName) {
    this->userName = userName;
}

void user::setPassword(std::string password) {
    this->password = password;
}

int main(){
    vector<user> userDetails;
    string line;
    string userName;
    string password;

    ifstream readFile("userInfo.txt");

    while(getline(readFile,line))   {
        stringstream iss(line);
        iss >> userName >> password;

        user userInfoDetails(userName,password);
        userDetails.push_back(userInfoDetails);

    }
    readFile.close();

    cout << "Before Deletion" << endl;
    for (int i =0; i<userDetails.size(); i++) {
       cout << userDetails[i].getUserName() << " " << userDetails[i].getPassword() << "\n";
    }
    cout << " " << endl;

    string name;
    cout << "Enter User Name to delete: ";
    cin >> name;
    cout << " " << endl;
    cout << "After Deletion" << endl;
    for (int i =0; i<userDetails.size(); i++) {
       if(userDetails[i].getUserName() == name){
            userDetails.erase(userDetails.begin() + i);
       }
        cout << userDetails[i].getUserName() << " " << userDetails[i].getPassword() << "\n";
    }
}

```