I need help with C++ my goal is to search for a word in a vector array and then return it as found!! someone please help me clarify this method without using linear search or binary search!! just a simple scan through the file and return it as found!
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <conio.h>
#include <cstring>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int searchVector(vector<string> QueryArray, string value){
for (int i = 0; i < QueryArray.size(); ++i)
if (QueryArray[i] == value)
return i;
return -1;
}
int main()
{
string line;
vector<string> QueryArray;
ifstream myfile("colleges.txt");
if(!myfile) //Always test the file open.
{
cout << " Error opening " << endl;
system("pause");
return 1;
}
while (getline(myfile,line))
{
QueryArray.push_back(line);
}
cout << QueryArray[4564] << endl;
int Colleges;
cout << "Welcome to My College and University summary program" << endl;
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 or universities appear in the state of your choice" << endl;
cout << "Press 3 to find out how many colleges and universities appear in our list in total" << endl;
cout << "Press 4 to quit" << endl;
cin >> Colleges;
if (Colleges == 1)
{
string search_word;
cout << "What University would you like to find?" << endl;
cin >> search_word;
getline(cin, search_word);
if (std::find(QueryArray.begin(), QueryArray.end(), search_word)!= QueryArray.end())
{
returntrue;
returnfalse;
cout << "The word was found*** this part is not working properly***" << search_word << endl; }
else {
cout << "---------The Matrix not found*** its not finding the words correctly*** ------------ " << endl;
}
}
elseif (Colleges == 2)
{
{
string line2;
int SIZE = 4564;
string STATE[SIZE];
ifstream file2 ("states.txt");
if (file2.is_open())
{
int i = 0;
while (file2 >> line2 )
{
STATE[i] = line2;
i++;
}
file2.close();
}
else cout << "Unable to open file";
}
constint numStates = 4564;
string STATES[numStates];
//find out the number of any particular state in the array
cout << "Enter a state's abbreviation:" << endl;
string userState;
cin >> userState;
int stateCount = 0;
for(int i = 0; i < numStates; i++){
if(STATES[i] == userState)
{
stateCount++;
}
}
cout << userState << " has " << stateCount << " colleges." << endl;
}
else
cout << "There are 4564 Universities available to pick from!" << endl;
return 0;
}
Change line 19 to if (QueryArray[i].compare(value) == 0). You can't use double equals signs to compare std::strings to each other, because they are objects, not primitive types. The compare function returns 0 if both strings are the same, so that is what you need to use instead. Let me know if you still have problems.
> You can't use double equals signs to compare std::strings to each other,
> because they are objects, not primitive types.
Yes, you can, std::string has an overloaded operator==
1 2 3 4 5 6 7
if (std::find(QueryArray.begin(), QueryArray.end(), search_word)!= QueryArray.end())
{
returntrue; //this will end the function
returnfalse; //never executes
cout << "The word was found*** this part is not working properly***" << search_word << endl; //never executes
}
okay so i changed the line 69-76 -- please explain to me what im missing at thi point so i can make the function read the vector and make the right connections between the string and the vector array!!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
{
string search_word;
cout << "What University would you like to find?" << endl;
cin >> search_word;
getline(cin, search_word);
int found = search_word(QueryArray, value);
if (found >= 0){
cout << "The word was found*** this part is not working properly***" << search_word << endl; }
{
else{ cout << "---------The Matrix not found*** its not finding the words correctly*** ------------ " << endl;
}
}