0

I have created a function that reads from a text file, adds content to a vector, shows the contacts. Then it prompts the user to choose which contact to remove. The program removes the contact but it removes other contacts as well (not all of them). Here is the code:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstdio>
#include <iomanip>

 using namespace std;

vector<string> contakt; 

void deleteContact() 
 {

    ifstream input;
    input.open("contacts.txt");
    string entry;
    int contactID=0;
    int index = contactID;

    while (getline(input, entry))
        {                   
            contakt.push_back(entry);

        }
    input.close();

    cout << "\n\n\nCurrent contacts in list: "<< endl;
    if (contakt.size() == 0) cout << "Empty" <<endl;
    for (int i = 0; i < contakt.size(); i++) 
        {
            cout << i << ") " << contakt[i] << endl;
        }
    cout<< " Enter the Id of the contact you would like to remove"<<endl;
    cin>> contactID;



    if (index != -1) 
        {
            ofstream output;
            output.open("temp.txt");
            for (vector<string>::iterator it = contakt.begin(); it!= contakt.end(); it++)
                { 
                contakt.erase(contakt.begin() + index);
                output<< *it <<'\n';
                }
            remove("contacts.txt");
            rename("temp.txt", "contacts.txt");
            output.close();
            cout << "Contact deleted succesfull." << endl;

        } 

        else cout << "\nNote: Id was not found in file." <<endl;

    return;
  }

code here

I remade the function from the beggining and now i am facing another problem. At the end of the file a blank space is created whenever i remove a contact.

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

 using namespace std;

 void deleteContact()
   {
    vector<string> file;
    string temp;

    ifstream input("contacts.txt");

    while( !input.eof() )
        {
            getline(input, temp);
            file.push_back(temp);

        }

    input.close();

    string item;

    cout << "Enter an name to delete from the contacts: ";
    cin>>item;

    int t = 0;

    for(int i = 0; i < (int)file.size(); ++i)
        {
            if(file[i].substr(0, item.length()) == item)
                {   
                    file.erase(file.begin() + i);
                    cout << "Order erased!"<< endl;
                    i = 0; // Reset search
                    t++;

                }

        }

    if (t == 0) cout<< "There is no contact with that name!!"<< endl;

    ofstream output("contacts.txt", ios::out | ios::trunc);

    for(vector<string>::const_iterator i = file.begin(); i != file.end(); ++i)
        {
            output << *i << '\n';
        }
output.close();
return;
}
Dimitrios Sria
  • 383
  • 1
  • 3
  • 12

1 Answers1

2

Your code modifies the vector while it iterates over it. This invalidates the iterator. Retrieve the updated iterator returned from the erase() function.

More details here: iterate vector, remove certain items as I go

Or, as you seem to delete just one contact at a time, just break your for loop after the first call to erase.

Community
  • 1
  • 1
Cee McSharpface
  • 8,493
  • 3
  • 36
  • 77