Question

This is for a class in C++ Problem 1: A class Names reads the following names...

This is for a class in C++

Problem 1: A class Names reads the following names from the .txt file

Name.txt

[Insert a long list of first, middle and last names. Each set of names is placed on a different line like the following:

John Doe

Jane Doe

Johnie Boy

etc.]

Make a search directory using dynamic allocation (pointers) where the user would search the any number

of letters of the first name and the matching name/names would come up as output. (Marks 20)

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <fstream>
#include <iostream>
#include <string>
#define MAX_NAMES 1000
using namespace std;

// Name class
class Name {
private:
  string fname;
  string lname;

public:
  // constructors
  Name() {}
  Name(string f, string l) {
    fname = f;
    lname = l;
  }
  // getter and setters
  string getFirstName() { return fname; }
  string getLastName() { return lname; }
  void setFirstName(string name) { fname = name; }
  void setLastName(string name) { lname = name; }
};

int main(int argc, char const *argv[]) {
  Name *names = new Name[MAX_NAMES];
  int count = 0;
  ifstream fin;
  // TODO: change the path of file
  string file_path = "Name.txt";
  fin.open(file_path, ios::in);
  if (!fin) {
    cout << "Error in opening file " << file_path << endl;
    exit(-1);
  }
  string f, l;
  // Reading line by line
  while (fin >> f >> l) {
    Name n(f, l);
    // adding to pointer array
    names[count++] = n;
  }
  string choice;
  while (1) {
    cout << "1. search name" << endl
         << "2. Exit\n"
         << "Enter Youe chioce : ";
    getline(cin, choice, '\n');
    if (choice == "1") {
      string temp;
      cout << "Enter first name : ";
      getline(cin, temp, '\n');
      cout << "\n------Names--------\n";
      // searching for name
      for (int i = 0; i < count; i++) {
        string fn = names[i].getFirstName();
        std::size_t index = fn.find(temp);
        if (fn.find(temp) != std::string::npos) {
          cout << names[i].getFirstName() << " " << names[i].getLastName()
               << endl;
        }
      }
      cout << endl;
    } else if (choice == "2") {
      exit(0);
    } else {
      cout << "invalid choice!\n";
    }
  }

  return 0;
}

//OUT

1. search name
2. Exit
Enter Youe chioce : 1
Enter first name : John

------Names--------
John Doe

Add a comment
Know the answer?
Add Answer to:
This is for a class in C++ Problem 1: A class Names reads the following names...
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
  • A class Names reads the following names from the .txt file Name.txt Samuel B Adams Noah...

    A class Names reads the following names from the .txt file Name.txt Samuel B Adams Noah Mark Aliso nKaleb Ashmore Kamsiyochokwu Chibueze Chiobi(Kamsi) Donald Wayne Bogen III Chrsitopher Blake Bourn William Lane Burkes Eric C Cebrian Sidney Walter Ceram iEddie L Cooper II Mathew Christopher Cropper Cole Lawrence Crosby Orlando Diaz Robert Joseph Evans Bryan K Foucha Jeremiah Israel Gardner Dylan Andrew Gilmore James Bryce Hendrix Andrew Paul Hood Alexander Thomas Jacobs Taiya Marie Jarva Zachary Tyler Jones Yabin Khadka...

  • Create a class “Person” which includes first name, last name, age, gender, salary, and haveKids (Boolean)...

    Create a class “Person” which includes first name, last name, age, gender, salary, and haveKids (Boolean) variables. You have to create constructors and properties for the class. Create a “MatchingDemo” class. In the main function, the program reads the number of people in the database from a “PersonInfo.txt” file and creates a dynamic array of the object. It also reads the peoples information from “PersonInfo.txt” file and saves them into the array. In C# Give the user the ability to...

  • Homework 1- Merge Files: 1. Design a class named MergeFiles 1 Three file names are passed...

    Homework 1- Merge Files: 1. Design a class named MergeFiles 1 Three file names are passed as command-line arguments to MergeFiles as follows: java MergeFiles filel file2 mergedFile II. MergeFiles reads names stored in files file and file2 III. Merges the two list of names into a single list IV. Sorts that single list V. Ignores repetitions VI. Writes the sorted, free-of-repetitions list to a new file named mergedFile.txt VII. The names in all three files are stored as one...

  • Write a Java program that creates and manipulates a directory of names, telephone numbers. The following...

    Write a Java program that creates and manipulates a directory of names, telephone numbers. The following information will be stored for each person in the directory: - Name (Last, First) - Home telephone number You should keep the entire collection ordered by key value (the combination of last and first names). Your program should be able to perform the following basic functions: - Search and display the contents of a particular entry - Display the entire directory - Delete an...

  • Help in JAVA please Write a program that reads a list of students (first names only)...

    Help in JAVA please Write a program that reads a list of students (first names only) from a file. It is possible for the names to be in unsorted order in the file but they have to be placed in sorted order within the linked list. The program should use a doubly linked list. Each node in the doubly linked list should have the student’s name, a pointer to the next student, and a pointer to the previous student. Here...

  • Write a complete C++ program that defines the following class: Class Salesperson { public: Salesperson( );...

    Write a complete C++ program that defines the following class: Class Salesperson { public: Salesperson( ); // constructor ~Salesperson( ); // destructor Salesperson(double q1, double q2, double q3, double q4); // constructor void getsales( ); // Function to get 4 sales figures from user void setsales( int quarter, double sales); // Function to set 1 of 4 quarterly sales // figure. This function will be called // from function getsales ( ) every time a // user enters a sales...

  • Python Help Please! This is a problem that I have been stuck on.I am only suppose...

    Python Help Please! This is a problem that I have been stuck on.I am only suppose to use the basic python coding principles, including for loops, if statements, elif statements, lists, counters, functions, nested statements, .read, .write, while, local variables or global variables, etc. Thank you! I am using python 3.4.1. ***( The bottom photo is a continuation of the first one)**** Problem statement For this program, you are to design and implement text search engine, similar to the one...

  • How can I create Loops and Files on Java? • Create a program that reads a...

    How can I create Loops and Files on Java? • Create a program that reads a list of names from a source file and writes those names to a CSV file. The source file name and target CSV file name should be requested from the user • The source file can have a variable number of names so your program should be dynamic enough to read as many names as needed • When writing your CSV file, the first row...

  • Write a program in Java according to the following specifications: The program reads a text file...

    Write a program in Java according to the following specifications: The program reads a text file with student records (first name, last name and grade on each line). Then it prompts the user to enter a command, executes the command and loops. The commands are the following: "print" - prints the student records (first name, last name, grade). "sortfirst" - sorts the student records by first name. "sortlast" - sorts the student records by last name. "sortgrade" - sorts the...

  • Hi I how do i use a vector to search a array for a string that...

    Hi I how do i use a vector to search a array for a string that user has enter into the console. here is some of the instructions for the programing I am trying to work on The purpose of this program is to write a class whose job is to read up to 10,000 names into a string array. The class sorts the names and then the user can ask the class to find full names containing a name...

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