I wrote a program that determines what rank the name (popular one is the first) that user entered is.
The problem is that the output is supposed to show rank for both girls and boys at the same time. For example, If I put Justice, the output should be
Justice is ranked 406 among boys.
Justice is ranked 497 among girls.
But my program only shows one of them.
Please tell me what the problem is and what to do to fix it.
Thanks,
Here is the program I wrote.
#include <iostream>
#include<string>
#include<fstream>
#include <sstream>
using namespace std;
int main()
{
string arr[1000];
string name;
ifstream infile;
infile.open("babynames2004.txt");
while (!infile)
{
cout << "unable to open the file. Please try again." << endl;
return 1;
}
cout<< " Please enter the name you want to find.(Make sure to wirte an upper case letter for the first letter of the name.)" << endl;
cin >> name;
int flag = 0;
int i;
int s=0;
string line;
while (!infile.eof())
{
getline(infile,line);
istringstream iss(line);
s=0;
do
{
string word;
iss >> word;
arr[s]=word;
s++;
} while (iss);
for(i=0; i<3; i++)
{
if (arr[i] == name)
{
flag = i;
break ;
}
}
if(flag)
break;
}
if (flag)
{
cout <<name<<" is ranked "<< arr[0] <<" among ";
if(flag== 1)
cout<<"boys\n";
else
cout<<"girls\n";
}
else
{
cout<< "Sorry,"<<name<<" is not ranked in the 1000 "<<endl;
}
return 0;
}