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
}

Recommended Answers

All 4 Replies

This is all my code if you need it for reference:

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

using namespace std;

void readFile(string, vector<string>, ifstream &); //Function prototype for the reading the file function
void displayNames(vector<string>); //Function prototype for the displaying the names function
void addName(string, ofstream &, vector<string>); //Funtion protoype for the adding a name to the list function
void deleteName(string, ofstream &, vector<string>); //Function prototype for the deleting a name from the list function
void quitProgram(); //Funtion prototype for the exiting function

int main()
{
	char cInput; //Menu input
	ifstream inFile; //Input file
	ofstream outFile; //Outut file
	string strFileName; //File name
	vector<string> vecStudent; //Vector holding student names

	cout << "Please enter the data file name (with location): "; //Asks the user for a file location and the name of file
	cin >> strFileName; //User enters the file location and name

	while (inFile.fail()) //While the file does not open display error message and asks for another file name and location
	{
		cout << "--------------------------------------------------\n";
		cout << "Input file error!\n";
		cout << "Please enter the data file name (with location): ";
		cin >> strFileName;
	}

	readFile(strFileName, vecStudent, inFile); //Calls readFile function to read the file into the vector

	while (true)
	{
		cout << "----------------------------------------\n";
		cout << " Grade Report Program - Main Menu\n";
		cout << "----------------------------------------\n";
		cout << " Enter 1 to display ALL students names\n";
		cout << " Enter 2 to add a student name\n";
		cout << " Enter 3 to delete a student name\n";
		cout << " Enter 4 to SAVE and quit the program\n";
		cout << "----------------------------------------\n";
		cout << "Enter menu option: "; //Asks user for menu option
		cin >> cInput; //User inputs menu option

		switch (cInput)
		{
			case '1':
			displayNames(vecStudent); //call a function to handle this option
			break;
			case '2':
			addName(strFileName, outFile, vecStudent); //call a function to handle this option
			break;
			case '3':
			deleteName(strFileName, outFile, vecStudent);//call a function to handle this option
			break;
			case '4':
			quitProgram();//call a function to handle this option
			return 0;
			default:
			cout << "invalid input" << endl;
			break;
		}
	}
	return 0; //Return 0 to exit program
}

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
}

void displayNames(vector<string> vecNames) //Display the names funtion definition
{
	cout << "--------------------------------------------------\n";
	cout << "Display all student names...\n";

	for (int i = 0; i < vecNames.size(); i++)
		cout << vecNames[i] << endl; //Displays the names from the vector

	cout << "---- A total of " << vecNames.size() << " names ----\n";
}

void addName(string strFile, ofstream &oFile, vector<string> vecNames) //Add the name function definition
{
	string strFName; //First name
	string strLName; //Last name

	oFile.open(strFile.c_str()); //Opens the output file

	while (oFile.fail()) //While the output file does not open display error message and prompt user again for a first and last name
	{
		cout << "----------------------------------------\n";
		cout << "Output file error!\n";
		cout << "Enter the name to be added <First and Last Name>: ";
		cin >> strFName >> strLName;

		oFile.open(strFile.c_str()); //Tries to open file again
	}

	cout << "----------------------------------------\n";
	cout << "Enter the name to be added <First and Last Name>: "; //Asks user for the first and last name to be added
	cin >> strFName >> strLName; //User enters first and last name to be added

	vecNames.push_back(strFName+" "+strLName); //Adds the name entered into the vector

	for (int i = 0; i < vecNames.size(); i++)
		oFile << vecNames[i] << endl; //Put the names from the vector into the output file

	oFile.close(); //Closes the output file

	cout << "---- Name '" << strFName << " " << strLName << "' has been added.\n"; //Displays that the name has been added
}

void deleteName(string strFile, ofstream &oFile, vector<string> vecNames) //Delete the name funtion definition
{
	string strFName; //Initialize first name variable
	string strLName; //Initialize last name variable

	cout << "----------------------------------------\n";
	cout << "Enter the name to be deleted <First and Last Name>: "; //Ask use what name they want to delete
	cin >> strFName >> strLName;

	vecNames.pop_back(); //Take that name off of the vector

	oFile.open(strFile.c_str()); //Open the output file

	while (oFile.fail()) //While the output while does not open display and error message and ask the user again
	{
		cout << "----------------------------------------\n";
		cout << "Output file error!\n";
		cout << "Enter the name to be deleted <First and Last Name>: ";
		cin >> strFName >> strLName;

		oFile.open(strFile.c_str());
	}

	for (int i = 0; i < vecNames.size(); i++)
		oFile << vecNames[i] << endl; //Put the names from the vector into the output file

	oFile.close(); //Close the output file

	cout << "---- Name '" << strFName << " " << strLName << "' has been deleted.\n";
}

void quitProgram() //Quitting the program function definition
{
	cout << "--------------------------------------------------\n";
	cout << "Thanks for using the program. Program terminated.\n"; //Displays that the program has terminated
}

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
}

You need to pass the vector by reference. Passing a vector by value causes that function to make a separate copy for use inside your function. Which means that you're not even modifying the original vector anymore.

void readFile(string strFile, vector<string> &vecNames, ifstream &iFile)


Confusingly, you don't need to pass regular arrays by reference... that is done automagically. And although vectors are array-like, it's technically an object.

Okay, thank you so much!

I am having a problem with my deleting a name function, in which I used pop.back, and it takes the last names off of the vector. If I wanted to take a name off that is let's say in the middle of the vector, how would I do that?

Here is the code for the function:

void deleteName(string strFile, ofstream &oFile, vector<string> &vecNames) //Delete the name funtion definition
{
	string strFName; //Initialize first name variable
	string strLName; //Initialize last name variable

	cout << "----------------------------------------\n";
	cout << "Enter the name to be deleted <First and Last Name>: "; //Ask use what name they want to delete
	cin >> strFName >> strLName;

	vecNames.pop_back(); //Take that name off of the vector

	oFile.open(strFile.c_str()); //Open the output file

	while (oFile.fail()) //While the output while does not open display and error message and ask the user again
	{
		cout << "----------------------------------------\n";
		cout << "Output file error!\n";
		cout << "Enter the name to be deleted <First and Last Name>: ";
		cin >> strFName >> strLName;

		oFile.open(strFile.c_str());
	}

	for (int i = 0; i < vecNames.size(); i++)
		oFile << vecNames[i] << endl; //Put the names from the vector into the output file

	oFile.close(); //Close the output file

	cout << "---- Name '" << strFName << " " << strLName << "' has been deleted.\n";
}

Surprisingly enough, the vector class has a method, it's called erase(), that allows you to do just that. If you plan on writing code and using vectors I'd encourage you to get a reference that you can use to help figure this type of question out. cppreference.com is one such reference.

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.