Tuesday, 15 June 2010

c++ - Creating a menu using vectors, strings and arrays while reading from 2 text files -



c++ - Creating a menu using vectors, strings and arrays while reading from 2 text files -

alright, been trying couple of hours i'm not getting anywhere. first of all, have 2 text files need read different function simple enough. 1 of text files, college.txt, have add together vector of strings; other text file(states.txt), have add together states parallel arrays of strings.

the problem(this related college.txt file) have how compare strings user inputs strings within vector since have validate whether or not colleges/universities user inputs on list(and of course of study repeat until user decides quit that's simple plenty while loop).

note1: before ask, else/if empty statements empty because want focus on problem first , go on on program.

note2: ide i'm using codeblocks

#include <iostream> #include <fstream> #include <string> #include <vector> #include <cstdlib> using namespace std; bool doesstringequalvector(vector<string> total, string name) { (unsigned int i=0; < total.size(); ++i) { if (name == total[i]) homecoming true; } homecoming 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 << "\nwelcome college , university search program.\n"; cout << "\npress button continue.\n "; system("pause>nul"); { cout << "\npress 1 come in possible colleges , universities."; cout << "\npress 2 find out how many colleges , universities"; cout << " appear in state.\n"; cout << "press 3 find total amount of colleges and"; cout << " universities in our list. "; cout << "\npress 4 quit. "; cin >> choice; if(choice == 1) { { cout << "\nenter name of college/university. "; cin >> userinput; collegesuniversities(comparison); if(userinput != comparison) cout << "\nthis establishment isn't on out list.\n "; else cout << "\nthis establishment on list.\n"; cout << "\nwould homecoming 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 using program. "; cout << "have great day.\n "; } else { cerr << "\nerror, invalid input. "; cout << "only integers 1 through 4 allowed.\n"; } } while(choice != 4); }

wrap in method checks through vector:

bool isstringinvector(vector <string> collection, string item) { (int i=0; < collection.size(); ++i) { if (item == collection[i]) homecoming true; } homecoming false; }

you have scan through collection see if it's in there.

c++ arrays string vector fstream

No comments:

Post a Comment