Firstly, you're missing an #include <string>. You need that to use the standard 'string' on all standard compilers.
Secondly, you seem to be setting up the boy and girl names list at the beginning of the loop; surely that should be above the loop and not in it? We don't want to load the same data over and over again.
Also, that was likely where the issue was. I'm hoping it was as I don't have a data file to test with, and I don't know what the specific problem was; only a rough idea.
If you're using file streams, I'm guessing you know what functions are and how to make your own, I suggest you do so because your code's structure will improve significantly and it will be easier for you to debug.
Don't ask the user to press EXIT when they should be typing it.
Indentation style is up to you, but I suggest you keep it consistant with code blocks, as it is very hard to read strangely indented code.
I suggest this with minimal revision:
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
|
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
const int RANK = 1000;
int n;
int main()
{
ifstream names;
string target, boyname, girlname;
bool findBoy, findGirl;
int rank, i;
int rankArray[RANK];
string boyArray[RANK];
string girlArray[RANK];
vector<string> searchHistory;
names.open("babynames2004.txt");
for(i=0;i<RANK;i++){ //loads list of top 1000 baby boy and girl names into arrays
names >> rank >> boyname >> girlname;
rankArray[i]=rank;
boyArray[i]=boyname;
girlArray[i]=girlname;
}
while(true) {
cout << "Enter a name (or EXIT when done): ";
cin >> target;
cout << endl;
if(target=="EXIT") break;
for(n=0;n<RANK;n++){ //checks if name input is on list
if(target==boyArray[n]){
cout << target << " is ranked " << (n+1) << " in popularity among boys." << endl;
findBoy=true;
}
if(target==girlArray[n]){
cout << target << " is ranked " << (n+1) << " in popularity among girls." << endl;
findGirl=true;
}
}
if(findBoy!=true) cout << target << " is not ranked among the top 1000 boy names." << endl;
if(findGirl!=true) cout << target << " is not ranked among the top 1000 girl names." << endl;
}
return 0;
}
|
EDIT: Fluffed some syntax, fixed.