Question

In: Computer Science

A class Names reads the following names from the .txt file Name.txt Jason Butler Paul Cunningham...

A class Names reads the following names from the .txt file

Name.txt

Jason Butler

Paul Cunningham

Logan Bryant

Cody paul

Robert Vernon Ehlers III

Quincy James Ellefson

Currionte Evans

Gavin Scott French

Zach Tyler Goss

Noah Cosby

Jeremy Whittle

Ryan brown

Jonah Dalton Null

Cameron jones

Xavier Rhodes

Malcom Wesley

Jamarcus johnson

Bernard smith

Joseph Nettles

Danny Willaims

James wellington

William bolar

Make a search directory using dynamic allocation where the user would search the any number of letters of the first name and the matching name/names would come up as output. Write C++ code and pseudocode in a doc file

Solutions

Expert Solution

In order to solve this problem, one should need to know about ifstream, eof, search, and npos.

  • ifstream is a stream class that is used to read from external files.
  • eof stands for End of File that is in simple terms, it checks till last value.
  • search is either a function pointer or function object.
  • npos is used to indicate no matches if the user wants to search for something.

Name.txt file:

C ++ Code for searching from a text file:

//basic header package to run the program
#include<iostream>
#include<conio.h>
//Name is in string, so add string package
#include<string>
// fstream package is used to run ifstream Myfile that is used to read the file
#include<fstream>

using namespace std;

int main()
{
    // search is itself a function object that is inbuilt
    string search;
    //offset is further used to manipulate or find the characters according to search input
    int offset;
    //line is itself a Name in text file
    std::string line;

    //ifstream is stream class to read from files, declaring filename that is Myfile
    ifstream Myfile;
    //opening the file that is Name.txt
    Myfile.open("Name.txt");
    //asking the user to enter the Name whatever they want to search
    cout<< "Type the name you want to search" <<endl;
    //storing the search value that the user input
    cin >> search;

    //if Name.txt file is open
    if(Myfile.is_open())
    {
    // eof stands for end of file that is in this case last name is the end of file
    while(!Myfile.eof())
    {
        //getline scans the value with space
        getline(Myfile, line);
        //if line.find is not equal to npos, this means name has been found
        if((offset = line.find(search, 0)) != string::npos){
            std::cout<< "The Name has been found as: " << line <<endl;
        }
    }
    //closing the Name.txt file
    Myfile.close();
    }
    else
    std::cout<<"Could not find the Name" << endl;
    //if search name doesn't match in Name.txt names, then system will pause for sometime
    system("PAUSE");
    return 0;

}

The output of the code if the user searches for the name Jason Butler:

so this is how one can search from a text file.


Related Solutions

(PYTHON) Write a program that does the following: reads each line from a txt file and...
(PYTHON) Write a program that does the following: reads each line from a txt file and convert it to lowercase counts the number of instances of: the characters 'a', 'e','i','o' and 'u' in the file creates a new file of file type .vowel_profile print outs lines in the file indicating the frequencies of each of these vowels Example input/output files: paragraph_from_wikipedia.txt (sample input) link: https://cs.nyu.edu/courses/fall19/CSCI-UA.0002-007/paragraph_from_wikipedia.txt paragraph_from_wikipedia.vowel_profile (sample output) link: https://cs.nyu.edu/courses/fall19/CSCI-UA.0002-007/paragraph_from_wikipedia.vowel_profile Please help!
This is all in Python. Also, the names come from a .txt file that i have...
This is all in Python. Also, the names come from a .txt file that i have created with 3 names on it(Jim,Pam,Dwight). Main Function. Write a program where the user enters a name. Using the read function, check to see if the name is in the text document. If it is, respond back to the user the name is found within the list. Read Function. The read function should return the contents of the text document as a list. EXAMPLE...
C++ Write a program that reads candidate names and numbers of votes in from a file....
C++ Write a program that reads candidate names and numbers of votes in from a file. You may assume that each candidate has a single word first name and a single word last name (although you do not have to make this assumption). Your program should read the candidates and the number of votes received into one or more dynamically allocated arrays. In order to allocate the arrays you will need to know the number of records in the file....
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
I just finished this program where the program reads a text file of full names ,...
I just finished this program where the program reads a text file of full names , first and last name, and a zip code. It reads the items then stores the first name and last name and zipcodes as an objects and prints the items.   However, when i use my text file i get this error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1 " I notice this issue only happens when the names are...
1. create a .txt file and call it fruits.txt, add the following items to the file...
1. create a .txt file and call it fruits.txt, add the following items to the file Apple Banana Peach Strawberry Watermelon Kiwi Grape _______ 2. Write a program that does the following: - Reads the fruit.txt - Converts the contents into all uppercase - Writes the uppercased values into a text file called "UpperFruit.txt" OUTPUT SHOULD BE: APPLE BANANA PEACH STRAWBERRY WATERMELON KIWI GRAPE
This is a python file Reads information from a text file into a list of sublists....
This is a python file Reads information from a text file into a list of sublists. Be sure to ask the user to enter the file name and end the program if the file doesn’t exist. Text file format will be as shown, where each item is separated by a comma and a space: ID, firstName, lastName, birthDate, hireDate, salary Store the information into a list of sublists called empRoster. EmpRoster will be a list of sublists, where each sublist...
Write a program that opens the file: "Lab6A_Data", reads all the values from the file, and...
Write a program that opens the file: "Lab6A_Data", reads all the values from the file, and calculates the following: A) The number of values in the file B) The sum of all the values in the file (a running total) C) The average of all the values in the file. D) The minimum value. E) The maximum value. F) The range of the data set of values. G) The number of times the value: '357' occurrs in the file. Display...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT