Hello,

I have entered 6 random names into a text file. From my program, I accessed that file, and stored the 6 names into a vector. Now my question is, how do I delete every n number of names? I am having trouble figuring out how to keep "wrapping around" the vector. For example.

Enter the position to delete: 3

Mike Bill Alex Chris Bob Dan
Mike Bill Chris Bob Dan
Mike Bill Chris Bob
Mike Bill Bob
Mike Bob
Mike

Any help would be greatly appreciated.

Recommended Answers

All 11 Replies

So you WANT to wrap around? Or you don't?

Please post your code, input, expected output, and current (incorrect) output.

Dave

So you WANT to wrap around? Or you don't?

Please post your code, input, expected output, and current (incorrect) output.

Dave

Thank you for your reply David, I do want to wrap around. Say I enter 4, I want it to delete the fourth name, count four more, and delete that name. Since the vector only has 6 names though, it would count up to six, and then start all over again and delete name 2. Do you understand my explanation of what I'm looking for?

int main()
{
	string name;
	vector<string> names;
	int positionOne = 0;

	ifstream fin;
	fin.open("directory to file.txt");

	if(!fin)
	{
		cout << endl;
		cout << "Error opening file!" << endl;
	}

	else
	{
		getDataOne(fin);
	}

	fin.close();
	cin.ignore();
	cin.get();
	return 0;
}


void getDataOne(ifstream & fin)
{
	string name;
	vector <string> names;
	int positionOne;
	int position = 0;

	while (fin >> name)
		{
			names.push_back(name);
		}

	cout << "Enter the position to erase: ";
	cin >> positionOne;

	names[positionOne-1] = position;
	
	do
	{
		 for(int i = position; i < names.size(); i++)
			{
				cout << names[i] << " ";
			}

	cout << endl;
	names.pop_back();
	

	}while(!names.empty());
}

i would use a while loop for your loop and have a counter variable. i would start the loop at the first posistion to delete than increment it by the value you entered first. i would check after incrementing to see if i exceded the size of the vector and if so then you would subtract the size from the counter and it would give you your next posistion to delete.

i would use a while loop for your loop and have a counter variable. i would start the loop at the first posistion to delete than increment it by the value you entered first. i would check after incrementing to see if i exceded the size of the vector and if so then you would subtract the size from the counter and it would give you your next posistion to delete.

Thank you for your replay, I should also mention, that I need this to work for any input. If I were to enter 100. I need it to keep wrapping around through the vector until it finds the 100th name.

You may want to use the modulus (%) operator for getting the index of vector to delete. All you have to do is keep adding the position to delete entered by user. Hope this helps

You may want to use the modulus (%) operator for getting the index of vector to delete. All you have to do is keep adding the position to delete entered by user. Hope this helps

Could you please elaborate, maybe with code.

Hi.. first of all you should use a list instead of a vector.I don't know if it'll help but here it is.. study this for a bit.

#include <list>
#include <iostream>
#include <fstream>
using namespace std;

void pop_name(string);

int main (int argc,char* argv[])
  {
    argc=1;
    string str=argv[argc];
    pop_name(str);
  }

void pop_name(string name)
  {
    ifstream in("file.txt");    
    list<string> names;

    list<string>::iterator it=names.begin();
    istream_iterator<string> parse(in);
    istream_iterator<string> end_of_file;

    while (parse!=end_of_file)
      {
        if (*parse!=name) names.push_back(*parse);
        ++parse;
      }
    in.close();
    

    ofstream out;
    out.open("file.txt");
    for (it=names.begin();it!=names.end();++it)
      {
        out << *it << endl;
      }
    out.close()
    
  }

Hi.. first of all you should use a list instead of a vector.I don't know if it'll help but here it is.. study this for a bit.

Thank you for your reply, unfortunately, I have to use vectors.

sorry .. i thoght you wanted to pop names so .. here

#include <list>
#include <iostream>
#include <fstream>
using namespace std;

void pop_name(int);

int main (int argc,char* argv[])
  {
    argc=1;    
    pop_name(atoi(argv[argc])-1); // i was in a hurry .. sorry for that -1:D
  }

void pop_name(int index)
  {
    int count_pos=0;

    ifstream in("file.txt");    
    list<string> names;

    list<string>::iterator it=names.begin();
    istream_iterator<string> parse(in);
    istream_iterator<string> end_of_file;

    while (parse!=end_of_file)
      {        
        if (count_pos!=index)
          {
            names.push_back(*parse);          
          }
        count_pos++;
        ++parse;
      }
               
    ofstream out;
    out.open("file.txt");
    for (it=names.begin();it!=names.end();++it)
      {
        out << *it << endl;
      }
    out.close();    
  }

you can replace the list with a vector. i didn't use "remove()". But lists are more adequate for poping members from te middle of the array.

#include <vector>
#include <iostream>
#include <fstream>
using namespace std;

void pop_name(int);

int main (int argc,char* argv[])
  {
    argc=1;    
    pop_name(atoi(argv[argc])-1); // i was in a hurry .. sorry for that -1:D
  }

void pop_name(int index)
  {
    int count_pos=0;

    ifstream in("file.txt");    
    vector<string> names;

    vector<string>::iterator it=names.begin();
    istream_iterator<string> parse(in);
    istream_iterator<string> end_of_file;

    while (parse!=end_of_file)
      {        
        if (count_pos!=index)
          {
            names.push_back(*parse);          
          }
        count_pos++;
        ++parse;
      }
               
    ofstream out;
    out.open("file.txt");
    for (it=names.begin();it!=names.end();++it)
      {
        out << *it << endl;
      }
    out.close();    
  }

you can replace the list with a vector. i didn't use "remove()". But lists are more adequate for poping members from te middle of the array.

Thanks for all the help, but for what I'm working on, I cannot use a lot of this. The question I had was for a program I'm working on for one of my classes, an intro c++ class. We haven't covered a lot of what you're coding .begin, .end, etc... so I'm trying to figure out another way to complete this. Thanks for your time though!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.