firstly I admit I am asking this because of my homework
secondly, my c++ is still at a beginner level. so I need some help.
ok..going back to my question.
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.
this is my contents of my textfile formatted by(username password)
textfile(userInfo.txt)
amber abc
janet def
chris DEF
gerald AZC
my output of my codes(say if I choose to delete janet.seems ok)
Before Deletion
amber abc
janet def
chris DEF
gerald AZC
Enter User Name to delete: amber
After Deletion
amber abc
chris DEF
gerald AZC
my output of my codes(now if I try to delete gerald)
Before Deletion
amber abc
janet def
chris DEF
gerald AZC
Enter User Name to delete: amber
After Deletion
amber abc
janet def
chris DEF
gerald AZC
As you can see. if I try to delete the last element in the vector, it will not get deleted.
I hope someone can explain it to me and tell me what I should do. Thanks.
NOTE
** I am not allowed to use c++11 for my solution for my homework **
my full 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";
}
}