your do block : it should be
1 2 3
|
do{
//...
} while(/*...*/);
|
instead of do{}if(){}else if(){}else{}
You can just create a function to clean up your code for getting the input
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
int option()
{
int opt{};
bool correct_answer = false;
while (!correct_answer) {
cout << "Press 1 to enter possible colleges and universities and the program will tell you if they appear on the list." << endl;
cout << "Press 2 to find out how many colleges and universities appear in the state of your choice." << endl;
cout << "Press 3 to find out how many colleges and universities appear on our list in total." << endl;
cout << "Press 4 to quit." << endl;
cin >> opt;
if (opt > 0 && opt < 5) correct_answer = true;
}
return opt;
}
|
Than you might create functions for different optons (functon1, function2 ...) and write
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
//read those files here
bool done = false;
while (!done)
{
int opt = option();
switch (opt)
{
case 1: { function1(); break; }
case 2: { function2(); break; }
case 3: { function3(); break; }
case 4: { done = true; break; }
}
}
cout << "Good bye! Have a nice day!!!" << endl;
|
your read files functions should probably return vectors of those read strings so you dont have to read files every time different options is entered for example
Also you are creating vector of size 4565 and than adding new read elements using
push_back
.
This way for example if you have a vector with elements
0 0 0 0 and you are using push_back(5) resulting vector will be 0 0 0 0 5. In your case you are having 4565 emty strings followed by whatever you read from your file.
So if you are going to add elements with push back just create an empty vector without elements at all. It will expand itself as you continue to add an elements to the end of the vector using push_back.
Resulting function could look like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
vector<string> campusfunction(string)
{
ifstream campustxt;
campustxt.open("colleges.txt");
//could test if file was correctly opened
vector<string> campusvector;
for(string campus; getline(campustxt, campus);)
{
campusvector.push_back(campus);
}
return campusvector;
}
|
You should do similar with second file and once you have those vectors you can pass them by
const vector<string>&
to functions that need those data.