|
-
April 24th, 2010, 12:02 PM
#1
eliminating an entry from a vector without changing the order
THE FIRST PART OF THIS IS BACKGROUND INFO ABOUT THE PROGRAM
I'm trying to write a program that inputs names from a file into a vector. The user will enter a number greater than 0 to be the "goose". The goose will be removed from the list, and then a new goose will be selected by counting to the goose number again, starting counting with the next person in the list.
When ran completely through it would look something like this:
Enter the position of the GOOSE: 2
Tom Jan Eddy Bill
Tom Eddy Bill
Tom Eddy
Tom
----------------------------------------------------------------------------------------------------------------
I need to remove the name from the list without changing the order, so I was going to do a series of interchanges until the "goose" was moved to the last spot in the vector, and then just pop it off the vector. I was going to send the location of the goose to my interchange function, and swap names[entry] with names[entry+1] until it was at the end of the vector. However that will not work if the user enters a number greater than the number of names in the vector. If someone enters 8, and there are only 3 names in the vector, it needs to keep looping until it finds the 8th name (which would be the 2nd name in the list of 3).
Really I'm open to all around any logic that may help with this problem. How would I deal with the problem of having to do a roll over effect if the user enters a number greater than names.size() and how I could go about interchanging until the "goose" is the last entry in the vector.
It doesn't help that I just learned how to use vectors either. Below is my code at the moment, the wackGoose function was supposed to be doing the eliminating but its empty.
Code:
#include <iostream>
#include <string>
#include <iomanip>
#include <vector>
#include <fstream>
using namespace std;
//CONSTANTS AND PROTOTYPES SECTION
void introduction();
//Introduce program and programmer
void getNames(vector<string> names);
//fills the vector with names
void getGoose(int&goose);
//gets the gooseth entry from the user
void wackGoose(vector<string> names, int goose);
//eliminates the gooseth entry over and over
template <class myType>
void interchange(myType&a, myType&b);
//interchanges two names
int main()
{
introduction();
cout << endl << endl;
vector<string> names;
int goose;
getNames(names);
getGoose(goose);
wackGoose(names, goose);
cin.ignore();
cin.get();
return 0;
}
//DEFINITIONS SECTION
void getNames(vector<string> names)
//fills the vector with names
{
string name;
ifstream fin;
fin.open("C:/Users/Josh/Desktop/goosey.txt");
int count =0;
while(fin >> name)
{
names.push_back(name);
cout << names[count] << " ";
++count;
}
cout << endl;
}
void getGoose(int&goose)
//gets the gooseth entry from the user
{
do{
cout << "Enter the position of the GOOSE: ";
cin >> goose;
}while(goose<1);
}
void wackGoose(vector<string> names, int goose)
//eliminates the gooseth entry over and over
{
int entry;
entry = goose-1;
}
template <class myType>
void interchange(myType&a, myType&b)
//interchanges two names
{
myType temp = a;
a=b;
b=temp;
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|