I am having trouble with reading from a vector of strings

Here is my issue, the program will allow me to input new names and i can search for them just fine but it will NOT allow me to search for IDs that I input, however it does find IDs that are read from the file. (For the sake of length and relevance I've take out that part of the code.) I am so confused as to why it doesn't do that. I've checked the portion of the code that takes in names and IDs and i've checked the searching portion and it identical I can't find anything wrong with it. I've also checked to make sure that the IDs are loaded onto the vector... They are in there. So why the heck would it let me search for names I put in but not for IDs? I've been going crazy trying to figure this out.

About my code, first there are some lines in here like 238 that are commented out. I did that because it wasn't working so i tried using a basic binary search function instead and that's not working either. Start by hitting B it will let you create a vector that can be used to test case F.

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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include <iostream>
#include <string>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <cctype>

using namespace std;


void sortFunction(vector <string> &, vector <string> &, vector <string> &); 
void displayVector(vector <string> &, vector <string> &, vector <string> &); 
void displayMenu(); 
int binarySearchName(vector <string> &, string, int, int); 

int main()
{

	vector <string> vName, vID, vClass;
	int iStudents, iClass, iSearch;
	string sAccumulatedClass, sSelection, sFileName, sTemp, sFirstName, sLastName, sMiddleName, sFullName, sID, sClass, sSearchN, sSearchI, sQuestion, sPick;
	ofstream newStudentList;


	const char OPTION_READING = 'A', OPTION_ADDING = 'B', OPTION_DISPLAYING = 'C', OPTION_SORTING = 'D',
		OPTION_WRITING = 'E', OPTION_SEARCHING = 'F', OPTION_ENDING = 'G';

	ifstream inputFile;

	do
	{

		displayMenu();  
		
		cout << "\nPlease choose an option" << endl << endl;
		getline(cin >> ws, sSelection);

		while (sSelection.length() > 1)
		{
			cout << "Invalid entry, please try again: ";
			getline(cin >> ws, sSelection);
		}
		
		

		switch (sSelection[0])
		{



		case OPTION_ADDING:
		case 'b':

			cout << "How many students would you like to add?" << endl;
			cin >> iStudents;
			cin.ignore();

			for (int j = 0; j < iStudents; j++)
			{
				cout << "Student" << j + 1 << ":\n";
				cout << "Please enter the student Full Name: ";
				getline(cin, sFullName);
				vName.push_back(sFullName);
				
				cout << "Enter ID number ";
				getline(cin >> ws, sID);
				vID.push_back(sID);				
				cout << "How many classes would you like to add?";
				cin >> iClass;
                cin.ignore();

				sAccumulatedClass.clear();
				for (int k = 0; k < iClass; k++)
				{
					cout << "Please enter the student's Class?";
					getline(cin >> ws, sClass);
					sAccumulatedClass += sClass + ' ';	
				}
				vClass.push_back(sAccumulatedClass);
			}
			
			cout << endl;
			displayVector(vName, vID, vClass);
		    break;

		case OPTION_SORTING:
		case 'd':
			if (vName.size() == 0)
			{
				cout << "Invalid entry, please try again!";
				break;
			}
			else
			cout << "The Student List after Sorting:" << endl;
				cout << "\n";
			
				sortFunction(vName, vID, vClass);

				displayVector(vName, vID, vClass);
				break;



		case OPTION_SEARCHING:
		case 'f':
		do
		{
			
			cout << "Search Menu:" << endl;
			cout << "1. By Name\n";
			cout << "2. By Student ID\n \n";
			cout << "Would you like to search by Name or ID?" << " " << endl << endl;
			getline(cin >> ws, sPick);
			while (sPick.length() > 1 || sPick != "1" && sPick != "2")
			{
				cout << "Please enter 1 (search by Name) or 2 (search by ID) to continue: ";
				getline(cin >> ws, sPick);
			}

			cout << endl << endl;
			displayVector(vName, vID, vClass);


			
				if (sPick == "1")
				{
					cout << endl << endl;
					cout << "Please enter a Full Name to be searched" << endl;
					getline(cin >> ws, sSearchN);
					iSearch = binarySearchName(vName, sSearchN, 0, vName.size() - 1);
					if (iSearch != -1)
					{
						cout << "\n";
						cout << "The name " << sSearchN << " was found in the student list." << endl << endl;
						cout << left << setw(30) << vName[iSearch] << setw(20) << vID[iSearch] << setw(20) << vClass[iSearch] << endl << endl;
					}
					else
					cout << "The name:" << " " << sSearchN << " " << "was not found in The Student List." << endl << endl;
					cout << "would you like to search more?" << endl;
					cout << "Please enter Y/y for Yes and N/n for No:" << endl << endl;
					getline(cin >> ws, sQuestion);
					
		}


				else if (sPick == "2")
				{
					cout << endl << endl;
					cout << "Please Enter an ID to be searched:" << endl << endl;
					getline(cin >> ws, sSearchI);
					/*iSearch = binarySearchName(vID, sSearchI, 0, vID.size() - 1);
					if (iSearch != -1)*/
					if (binary_search(vID.begin(), vID.end(), sSearchI))
					{
						cout << "The ID:" << " " << sSearchI << " " << "was found in The Student List." << endl << endl;
						/*cout << left << setw(30) << vName[iSearch] << setw(20) << vID[iSearch] << setw(20) << vClass[iSearch] << endl << endl;*/
					}
					else
					cout << "The ID:" << " " << sSearchI << " " << "was not found in The Student List." << endl << endl;
					cout << "would you like to search more?" << endl;
					cout << "Please enter Y/y for Yes and N/n for No:" << endl << endl;
					getline(cin >> ws, sQuestion);
					
				}

				else
						
				while (sPick.length() > 1 || sPick != "1" && sPick != "2")
				{
					cout << "Please enter 1 (search by Name) or 2 (search by ID) to continue: ";
					getline(cin >> ws, sPick);
				}
			 
		} while (sQuestion == "Y" || sQuestion == "y");
			break;


		case OPTION_ENDING:
		case 'g':
			cout << "Thank you for using this program!";

			return 0;
			break;
	
		default:
			cout << "Error, invalid choice, try again!" << endl << endl;
		
		}
	} while (toupper(sSelection[0] != OPTION_ENDING));


}

//void function for calling menu when needed
void displayMenu()
{
	
	cout << "\t \t \t" "Student List menu" << endl << endl;
	cout << "A. Reading the Student List from a file\n";
	cout << "B. Adding Student's Informations into the Student List\n";
	cout << "C. Displaying the content of the Student List\n";
	cout << "D. Sorting and Displaying the content of the Student List\n";
	cout << "E. Writing the Student List to a file\n";
	cout << "F. Searching for a Student's Informations from the Student List\n";
	cout << "G. Ending the program" << endl << endl;


}

//Void function for calling vectors when needed
void displayVector(vector <string> &vName, vector <string> &vID, vector <string> &vClass)
{
	cout << "Now the Student List has the size of:" << " " << vName.size() << endl << endl;
	cout << left << setw(30) << "Name:" << setw(20) << "ID:" << setw(20) << "Enrolled Class : " << endl;
	cout << "--------------------------------------------------------------------------";
	cout << "\n \n";
	
	for (int i = 0; i < vName.size(); i++)
		cout << left << setw(30) << vName[i] << setw(20) << vID[i] << setw(20) << vClass[i] << endl;

}

//Void sorting fuction
void sortFunction(vector <string> &vName, vector <string> &vID, vector <string> &vClass)
{
	int startScan, minIndex;
	string minValue, minValueA, minValueB;
//loop for number of iterations
	for (startScan = 0; startScan < vName.size() - 1; startScan++)
	{
		minIndex = startScan; 
		minValue = vName[startScan]; 
		minValueA = vID[startScan]; 
		minValueB = vClass[startScan]; 


		for (int index = startScan + 1; index < vName.size(); index++)
		{

			if (vName[index] < minValue)
			{
				minValue = vName[index];
				minValueA = vID[index];
				minValueB = vClass[index];
				minIndex = index;

			}
		}

		vName[minIndex] = vName[startScan];
		vName[startScan] = minValue; 
		vID[minIndex] = vID[startScan];
		vID[startScan] = minValueA;
		vClass[minIndex] = vClass[startScan];
		vClass[startScan] = minValueB;

	}
}

//binary search function for vName
int binarySearchName(vector <string> & vName, string sSearch, int min, int max)
{
	while (min <= max) 
	{
		int mid = (min + max) / 2; 
		if (vName[mid] == sSearch) 
		{
			return mid; 
		}
		else if (vName[mid] < sSearch) 
		{
			return binarySearchName(vName, sSearch, mid + 1, max); 
		}
		else 
			return binarySearchName(vName, sSearch, min, mid - 1); 
	}
	return -1;
}

Last edited on
my guess is this thing you're doing with getline.

getline(cin>>variable,variable)
I've tried going through it with F11 step into and F10 step over watching the values of everything and it seems that everything loads correctly. the variable sSearchI contains whatever the user inputs and the vector vID has said variable when the compiler gets to the if(binary_search) statement it simply tests and moves on even though its in there.

The problem seems to be when i add case A. Please ignore/remove this thread
Last edited on
Topic archived. No new replies allowed.