November 25th, 2011, 02:42 AM
Reading a text file into a vector
I have an assignment where I have to write a program to prompt the user for a file name and location (it is a text file), once the user has entered that, a menu pops up and gives the user 4 options and asks which one they would like to do. Option 1 is to display all the names in the text file (there are 3 names, which have a first and last name with a space in between). The second option is to add a name to the file, the third option is to delete a name from the file and the fourth option is to exit and save the program, saving whatever names were added to or deleted from the file.
Now I have written out the code for the program, using 6 functions (including int main) the other five functions are: the read file into vector function, the display all names function, the add a name function, the delete a name function and finally the exit and save program function.
I seem to be having a problem with reading the names from the file into the vector, I am not exactly sure why either, seeing as I have followed how the teacher did it in class.
I will start by posting the readFile function and see if you guys can give me any help on it, I am desperate! I will take any help I can get, thank you!
void readFile(string strFile, vector<string> vecNames, ifstream &iFile) //Read the file into the vector function definition
string strFName, strLName; //First and last name
iFile.open(strFile.c_str()); //Opens file
while (iFile >> strFName >> strLName) //While the file is copying into the first and last names
vecNames.push_back(strFName + " " + strLName); //Push the names onto the back of the of the vector
iFile.close(); //Close the input file
Tags for this Thread
Click Here to Expand Forum to Full Width
This is a CodeGuru survey question.