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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
}
This is all my code if you need it for reference:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163

#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
}
You pass the vector of strings to your readFile function by value, which means that, at line 34 of your main function, you pass a copy of vecStudent, not the vector itself. The vector used inside readFile, where you store your strings, is a copy of the paramater you pass to it, so any changes are made to this copy, not the original vector.

You should pass the vector as a pointer or reference (I prefer the former):

1
2
3
void readFile(string strFile, vector<string> * vecNames, ifstream &iFile);
// ...
readFile(strFileName, &vecStudent, inFile);
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, I knew that it wouldn't work but I wasn't sure how else I'd do it. 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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";
}
To find the name, std::find(): http://cplusplus.com/reference/algorithm/find/
To remove from the vector, vector::erase(): http://cplusplus.com/reference/stl/vector/erase/
And, just as a performance note: http://cplusplus.com/reference/iostream/manipulators/endl/
Topic archived. No new replies allowed.