Well I can't off-hand see anything wrong with your logic, but your file reading loop should not test for eof(). The reason being that EOF is not flagged until after the program tries to read past the end of file. So if your getline() fails you are going to be using an invalid entry.
The recommended way to do this is:
1 2 3 4 5 6 7
/*Read File*/
while(std::getline(iFile, temp)) // Make getline() the condition
{
// Now you know that the line was successfully read
// and that valid data is going into your vector
file.push_back(temp);
}
I entered both input of the same name into the program.
Lets call the first input detail "a".
Second input "a".
However when I want to remove one of the detail, let's say the first detail.
They delete both of them since they share the same name.
Is there anyway to select one of the details so I can removed it?
Actually looking more closely at your code it seems a little inefficient. In your loop at line 22 to search for a detail to delete, you write the new file out to disk every time you delete something. There is no need to do that until *after* you have found and removed your elements.
Simply moving the loop closing brace at line 38 to line 30 should fix that.
Then it is simply a matter of breaking out of the loop after you delete the first element:
void reInc(){
vector<string> file;
string temp;
string des;
ifstream iFile("Income.txt");
/*Read File*/
while(!iFile.eof()){
getline(iFile,temp);
file.push_back(temp);
}
iFile.close();
/*User type in the details to delete*/
cout << "Enter DETAILS to delete: ";
getline(cin,des);
for(int i = 0; i < (int)file.size(); i++){
if(file[i].substr(0, des.length()) == des){
file.erase(file.begin() + i);
cout << "Detail Deleted" << endl;
break; // end loop after deleting first one found
}
} // end loop here
// Do this outside of loop
/*Update new list*/
ofstream oFile("Income.txt", ios::out | ios::trunc);
for(vector<string>::const_iterator i = file.begin(); i != file.end(); i++){
oFile << *i <<endl;
}
oFile.close();
}
I recommend doing the same for your other functions. Update the data in your vector completely before writing it to the file on disk. Do all the writing to file on disk outside the loops that update the data.