Question

I need help with this question for programming. The language we use is C++ The program...

I need help with this question for programming. The language we use is C++

The program should ask the user if they want to search for a boy or girl name. It should then ask for the name search the appropriate array for the name and display a message saying what ranking that name received, if any. The name files (BoyNames.txt and GirlNames.txt) are in order from most popular to least popular and each file contains two hundred names. The main method should ask if the user wants to search for a boys’ name or a girls’ name. It should then call a method that fills an array with the appropriate gender names. Then ask the user for the name and call a method that will accept the array and the name as parameters. This method will search the array for the name and return the ranking. The main should then report the ranking, if any.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ code for the given problem is copied below including screenshots. Name list of boys and girls have been kept in BoyNames.txt and GirlNames.txt files. Program follows the steps as given in instructions, i.e., asks user for gender, based on that input loads the nameList array from appropriate file, then asks user to specify the name to search in that array and finally searches through the array for that name and if found shows the ranking to the user.

Appropriate comments have been added and sample program output is also copied below.

========================================================================

#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

const int NAME_LIST_LENGTH = 200;

//Function to load name array passed by reference from specified name file
void loadNameArray(string* list, string filePath)
{
    ifstream nameFile(filePath);
    if (nameFile.is_open())
    {
        int i=0;
        string line;
        while (getline (nameFile,line) || i<NAME_LIST_LENGTH)
        {
          list[i] = line;
          i++;
        }
        nameFile.close();
    }
    else
        cout << "Unable to open file:" << filePath;
}

//Main function
int main()
{
    char gender;                                        //Stores input gender from user
    const string BOY_NAMES_FILE = "BoyNames.txt";       //Constant string to hold boy name file path
    const string GIRL_NAMES_FILE = "GirlNames.txt";     //Constant string to hold boy name file path
    string nameList[NAME_LIST_LENGTH];                  //String array to store NAME_LIST_LENGTH (200) names from file
    string name, nameCopy;                              //Constant string to hold boy name file path
  
    //Ask user for which gender he/she wants to search names for
    cout << "Search for a boys' or girls' name (specify 'B' or 'G'): ";
    cin >> gender;
  
    //Load appropriate file, i.e., either for boys or girls depending upon gender specified by user
    if(gender == 'B' || gender == 'b')
    {
        loadNameArray(nameList, BOY_NAMES_FILE);
    }
    else if(gender == 'G' || gender == 'g')
    {
        loadNameArray(nameList, GIRL_NAMES_FILE);
    }
  
    //Ask user for the name he/she wants to search and find ranking in the array
    cout << "Enter the name: ";
    cin >> name;
    nameCopy=name; //Saves a copy of name for displaying in final output ranking string as original variable will be transformed to lower case
  
    //Transform name to lower case for comparision
    transform(name.begin(), name.end(), name.begin(), ::tolower);
  
    //Run a loop NAME_LIST_LENGTH (200) times to search through loaded nameList for user specified name
    for (int i=0; i<NAME_LIST_LENGTH; i++)
    {
        //Transform nameList[i] to lower case for comparision with name
        transform(nameList[i].begin(), nameList[i].end(), nameList[i].begin(), ::tolower);
        if(nameList[i].compare(name) == 0) //Compare nameList[i] and name, 0 means equal
        {
            //Name found, display the ranking to the user
            cout << "Name " << nameCopy << " is found to be ranked at: " << i+1 << ".";
            //End the program
            exit(0);
        }
    }
  
    //Name not found since code is still running
    cout << "Name " << nameCopy << " not found in the list.";
  
    return 0;
}

========================================================================

Screenshot:

========================================================================

Sample Output:

Add a comment
Know the answer?
Add Answer to:
I need help with this question for programming. The language we use is C++ The program...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need help with the code for this assignment. Python Assignment: List and Tuples We will do...

    Need help with the code for this assignment. Python Assignment: List and Tuples We will do some basic data analysis on information stored in external files. GirNames.txt contains a list of the 200 most popular names given to girls born in US from year 2000 thru 2009: (copy link and create a txt file): https://docs.google.com/document/d/11YCVqVTrzqQgp2xJqyqPruGtlyxs2X3DFWn1YUb3ddw/edit?usp=sharing BoyNames.txt contains a list of the 200 most popular names given to boys born in US from year 2000 thru 2009 (copy link and create...

  • The text files boynames.txt and girlnames.txt, which are included in the source code for this boo...

    The text files boynames.txt and girlnames.txt, which are included in the source code for this book, contain lists of the 1,000 most popular boy and girl names in the United States for the year 2005, 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...

  • Visual Basic Question In the Chap9 folder of the student sample programs, you will find the...

    Visual Basic Question In the Chap9 folder of the student sample programs, you will find the following files: GirlNames.txt - This file contains a list of the 200 most popular names given to girls born in the United States from 2000 to 2012 BoyNames.txt - This file contains a list of the 200 most popular names given to boys born in the United States from 2000 to 2012 Create an application that reads the contents of the two files into...

  • The code will not run and I get the following errors in Visual Studio. Please fix the errors. err...

    The code will not run and I get the following errors in Visual Studio. Please fix the errors. error C2079: 'inputFile' uses undefined class 'std::basic_ifstream<char,std::char_traits<char>>' cpp(32): error C2228: left of '.open' must have class/struct/union (32): note: type is 'int' ): error C2065: 'cout': undeclared identifier error C2065: 'cout': undeclared identifier error C2079: 'girlInputFile' uses undefined class 'std::basic_ifstream<char,std::char_traits<char>>' error C2440: 'initializing': cannot convert from 'const char [68]' to 'int' note: There is no context in which this conversion is possible error...

  • Please create a program answering the following question in C PROGRAMMING. CorrectFormation Create a program and...

    Please create a program answering the following question in C PROGRAMMING. CorrectFormation Create a program and locally declare in main fname and Iname and completeName. Ask the user for their first and last name. Put the values into fname and Iname. You will create a function that is called as followed oinNames( fname, Iname, completeName ); You will pass the first name array, the last name array, and the array that will contain the complete name. The function named joinNames...

  • Need C programming help. I've started to work on the program, however I struggle when using...

    Need C programming help. I've started to work on the program, however I struggle when using files and pointers. Any help is appreciated as I am having a hard time comleting this code. #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LINE 100 #define MAX_NAME 30 int countLinesInFile(FILE* fPtr); int findPlayerByName(char** names, char* target, int size); int findMVP(int* goals, int* assists, int size); void printPlayers(int* goals, int* assists, char** names, int size); void allocateMemory(int** goals, int** assists, char*** names, int size);...

  • C++ Programming

    PROGRAM DESCRIPTIONIn this project, you have to write a C++ program to keep track of grades of students using structures and files.You are provided a data file named student.dat. Open the file to view it. Keep a backup of this file all the time since you will be editing this file in the program and may lose the content.The file has multiple rows—each row represents a student. The data items are in order: last name, first name including any middle...

  • Write a C++ program that produces the following output: /* OUTPUT Enter your age: 21 Enter...

    Write a C++ program that produces the following output: /* OUTPUT Enter your age: 21 Enter the last name: Lee Hello Tom Lee. You are 21 years old. Press any key */   Declare an array named: firstName The array is a c_string, i.e., it is a null-terminated character array. The size of the array is 10. Assign a first name to it when it is declared.   Declare an array named: lastName The array is a c_string, The size of the...

  • Program with generic merge sort and binary search method help. The programming language I'm using is...

    Program with generic merge sort and binary search method help. The programming language I'm using is Java. This program should show understanding generic merge sort methods and generic binary search methods in java. The execution should include at least 5 found items including one from the first three items in the sorted array and one from the last three items in the sorted array as well as at least two items not found Create a generic merge sort method that...

  • (c programming): write a program that contains a function named getName, that does not get any...

    (c programming): write a program that contains a function named getName, that does not get any parameter and returns a character array of a random name from a constant list of 30 names, in a global array. the function returns that random name. each name in the list is a character string that consists of not more than 20 characters(not including finishing 0). the name consists of only characters from the american alphabet (a-zA-Z) and does not contain any "white"...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT