Alright, I been trying to do this for a couple of hours but I'm not getting anywhere. First of all, I have 2 text files that I need to read from a different function which is simple enough. Then for one of the text files, mainly the college.txt, I have to add it to a vector of strings; for the other text file(states.txt), I have to add the states to parallel arrays of strings.
The problem(This is related to the college.txt file) I have is how do I compare the strings the user inputs to the strings inside the vector since I have to validate whether or not the colleges/universities that the user inputs are on the list(and of course repeat it until the user decides to quit but that's also simple enough to do with a while loop).
Note1: Before you ask, The else/if with empty statements are empty because I want to focus on this problem first and then I will continue on with the program.
Note2: The IDE that I'm using is CodeBlocks
New problem-Whenever I input the name of a college or university that I know is in the list, it causes an infinite loop to occur and if that doesn't happens it outputs the cout statement "Error, invalid input" multiple times.
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
|
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
using namespace std;
bool DoesStringEqualVector(vector<string> total, string name)
{
for (unsigned int i=0; i < total.size(); ++i)
{
if (name == total[i])
return true;
}
return false;
}
void collegesUniversities(string)
{
ifstream campuses;
campuses.open("colleges.txt");
string schools;
vector<string> schoolVector;
if(!campuses)
cerr << "Error opening file. ";
else
{
while(campuses.good())
{
getline(campuses,schools, '\n');
schoolVector.push_back(schools);
cout << schools << endl;
}
}
DoesStringEqualVector(schoolVector, schools);
campuses.close();
}
int main()
{
char response;
string comparison;
int choice;
string userInput;
cout << "Welcome to my college and university search program.\n";
cout << "\nPress any button to continue.\n ";
system("pause>nul");
do
{
cout << "\nPress 1 to enter possible colleges and universities.";
cout << "\nPress 2 to find out how many colleges and universities";
cout << " appear in your state.\n";
cout << "Press 3 to find the total amount of colleges and";
cout << " universities in our list. ";
cout << "\nPress 4 to quit. ";
cin >> choice;
if(choice == 1)
{
do
{
cout << "\nEnter the name of your college/university. ";
cin >> userInput;
collegesUniversities(comparison);
if(userInput != comparison)
cout << "\nThis institution isn't on out list.\n ";
else
cout << "\nThis institution is on the list.\n";
cout << "\nWould you like to return to the menu?[Y/N] ";
cin >> response;
while(response != 'Y' && response != 'y' && response != 'N' &&
response != 'n')
{
cerr << "\nError, Invalid Input. ";
cin >> response;
}
}
while(response != 'N' && response != 'n');
}
else if(choice == 2)
{
}
else if(choice == 3)
{
}
else if(choice == 4)
{
cout << "\nThank you for using my program. ";
cout << "Have a great day.\n ";
}
else
{
cerr << "\nError, Invalid input. ";
cout << "Only integers from 1 through 4 are allowed.\n";
}
}
while(choice != 4);
}
|