Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The text files boynames.txt and girlnames.txt contain a list of the 1,000 most p

ID: 3565764 • Letter: T

Question

The text files boynames.txt and girlnames.txt contain a list of the 1,000 most popular boy and girl names in the United States for the year 2003 as compiled by the Social Security Administration.

These are blank-delimited files where the most popular name is listed first, the second most popular name is listed second, and so on to the 1,000th most popular name, which is listed last. Each line consists of the first name, followed by a blank space, followed by the number of registered births in the year using that name. For example, the girlnames.txt file begins with:

Emily 25494
Emma 22532
Madison 19986

This indicates that Emily is the most popular name with 25,494 registered namings, Emma is the second most popular with 22,532, and Madison is the third most popular with 19,986.

Write a program that reads both the girl

Explanation / Answer

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
void readFile1();
void readFile2();

int main () {
readFile1();
readFile2();

return 0;
}

void readFile1()
{
    string line;
ifstream myfile ("girls.txt");
    size_t pos;
    string word = "Amya";
    string name,rank;
    int count =0;
    bool flag = false;
if (myfile.is_open())
{
    while ( myfile.good() )
    {
        count++;
      getline (myfile,line);
       pos=line.find(word); // search
      if(pos!=string::npos) // string::npos is returned if string is not found
        {
         flag = true;
         stringstream ssin(line);
         ssin>>name;
         ssin>>rank;
            cout <<name<<" is ranked "<<count<<" in popularity among girls with "<<rank<<" namings. ";
       //     cout<< sub<<"namings.";
            break;
        }
       
        if(!flag)
        {
            cout<<word<<" is not ranked among the top 1000 girl names"<<endl;
        }
     
    }
    myfile.close();
}

else cout << "Unable to open file";
}


void readFile2()
{
    string line;
ifstream myfile ("boys.txt");
    size_t pos;
    string word = "Amya";
    string name,rank;
    int count =0;

    bool flag = false;
if (myfile.is_open())
{
    while ( myfile.good() )
    {
        count++;
      getline (myfile,line);
       pos=line.find(word); // search
      if(pos!=string::npos) // string::npos is returned if string is not found
        {
         flag = true;        
         stringstream ssin(line);
         ssin>>name;
         ssin>>rank;
            cout <<name<<" is ranked "<<count<<" in popularity among boys with "<<rank<<" namings.";
       //     cout<< sub<<"namings.";
            break;
        }

              if(!flag)
              {
                 cout<<word<<" is not ranked among the top 1000 girl names"<<endl;
             }     
    }
    myfile.close();
}

else cout << "Unable to open file";
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote