Boolean function not evaluating correctly.

I'm trying to create a vector of strings from a text file. Then compare user input to the vector. It is not evaluating correctly and always returning a false bool value.
Another problem is the vector list not writing to a new file. It just creates a blank one.

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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;


void selectionSort(vector<string> &);
vector<string>getVector(const string&);
string getName(const string&);
bool search(const string& , vector<string>&);
void displayResult(const string&, const string&, bool);
void writeToFile(const string&, vector<string>&);
void reverseVector(vector<string>& vec);


int main()
{
   string boyName, girlName;   
   bool boyNameFound, girlNameFound;  

   
   vector<string> boyNames(getVector("BoyNames.txt"));
   vector<string> girlNames(getVector("GirlNames.txt"));
   
   
   boyName = getName("boy's");   
   girlName = getName("girl's");
   
   selectionSort(boyNames);  
   selectionSort(girlNames);
   
   boyNameFound = search(boyName, boyNames); 
   girlNameFound = search(girlName, girlNames);
   
   displayResult("boy's", boyName, boyNameFound);    
   displayResult("girl's", girlName, girlNameFound);
   
   writeToFile("Boynames_asc.txt", boyNames); 
   writeToFile("Girlnames_asc.txt", girlNames);
   
   reverseVector(boyNames); 
   reverseVector(girlNames);

   writeToFile("Boynames_desc.txt", boyNames); 
   writeToFile("Girlnames_desc.txt", girlNames);
   
   cout<<endl;
   
   //system("PAUSE");
   return 0;
}


void selectionSort(vector<string> &arr)
{
	
	int startScan, minIndex;
	string minValue;

   for (startScan = 0; startScan < (arr.size() - 1); startScan++)
   {
      minIndex = startScan;
      minValue = arr[startScan];
      for(int index = startScan + 1; index < arr.size(); index++)
      {
         if (arr[index] < minValue)
         {
            minValue = arr[index];
            minIndex = index;
         }
      }
      arr[minIndex] = arr[startScan];
      arr[startScan] = minValue;
   }
	
	
}
vector<string>getVector(const string& filename)
{
    ifstream file_obj;
    string name;
    vector<string>vec;
    file_obj.open(filename);
    
    while (file_obj >> name)
    {
        getline(file_obj, name);
        vec.push_back(name);
    }
    
    file_obj.close();
    return vec;

}

string getName(const string& gName)
{
    string name;
    cout << "Enter a " << gName << " name, or N if you do not wish to enter a name: ";
    cin >> name; 
    return name;
}

bool search(const string& name, vector<string>& vec)
{
    for (int i = 0; i < vec.size(); i++)
    {
        if (vec[i] == name)
            return true;
    }
    return false;
    
}
void displayResult(const string& gName, const string& inName, bool popular)
{
    if (popular)
        cout << endl << inName << " is a popular " << gName << " name. ";
    else
        cout << endl << inName << " is not a popular " << gName << " name. ";
}
void writeToFile(const string& filename, vector<string>&vec)
{
    ofstream vecOut;
    vecOut.open("Vector_output.txt");
    for (int i = 0; i < vec.size(); i++)
    {
        vecOut << vec[i] << endl;
    }
    vecOut.close();
}
void reverseVector(vector<string>& vec)
{
    string name;
    for (int i = 0, j = vec.size() - 1; i < vec.size()/2; i++, j--)
    {
        name = vec[i];
        vec[i] = vec[j];
        name = vec[j];
    }
}

string == looks for an exact match. the function you wrote looks good, but if the user input has an extra space or different capitalization or whatnot it will NOT match. you can 'to lower' or to upper both sides if you want to, and you can write a 'trim' to pull off leading or trailing spaces, and so on...

write to file... change the file write to cout to and debug it.

debug/validate everything in main. For example, if selection sort trashes your vector, then nothing after it will work if the vectors are empty, and they will silently do nothing and give confusing results.
Last edited on
I tried to enter the same as the txt file still evaluating false.

Enter a boy's name, or N if you do not wish to enter a name: John
Enter a girl's name, or N if you do not wish to enter a name: Amy

John is not a popular boy's name.
Amy is not a popular girl's name.
@trmbeef,
You have fallen into the trap of trying to write a whole mass of code before testing any of it. You should develop and test code slowly - one function at a time.

Your code fails at the first function call in main() - you completely fail to read the file correctly. You should have checked that before writing any other code (e.g. by printing the contents of your vector.)

The offending lines ...
1
2
3
4
5
    while (file_obj >> name)
    {
        getline(file_obj, name);
        vec.push_back(name);
    }


The while >> statement reads a string into name ... leaving the '\n' line feed in the stream ... which is picked up by getline(), putting a blank in name ... which is what you then put in your vector.

You could simply remove the getline() statement here, as you already have a value in name.


Develop ONE FUNCTION AT A TIME, not present a wall of code. Don't worry ... you have plenty more errors later on!
Last edited on
For reverse vector, you could just use std::reverse() http://www.cplusplus.com/reference/algorithm/reverse/

Also, unless you have to write a selection sort, there is std::sort() http://www.cplusplus.com/reference/algorithm/sort/

getName() asks to enter N if not wish to enter a name, but this isn't tested in the caller.

I suggest you also code a routine to display the contents of a vector (display ?) then you can call this at various points in the program to check the vector values are what are expected from the input.
Thanks for all the help I was able to resolve. I think going function by function is a good idea to catch mistakes.
Topic archived. No new replies allowed.