SEARCHING FOR A WORD IN A VECTOR ARRAY AND RETURN IT AS FOUND!!

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!


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

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <conio.h>
#include <cstring>
#include <cstdlib>
#include <ctime>

using namespace 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())
{
return true;

return false;
        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;
}
}
else if (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";
}

   const int 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;
}

Last edited on
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.

Edit: I was wrong; see ne555's reply below.
Last edited on
> 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())
{
   return true; //this will end the function

   return false; //never executes
   cout << "The word was found*** this part is not working properly***" << search_word <<  endl; //never executes
}
Also, learn to indent
Last edited on
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;
}
}
Topic archived. No new replies allowed.