Question
Using C++, Create a file named names.txt, which contains a list of names. The names are of various length but no more than 100 characters,
Using C++,
Create a file named names.txt, which contains a list of names. The names are of various length but no more than 100 characters, and each name takes one line. Your program should be able to: display the first five names, count the total number of names, display the last five names, and copy all names that contains a lower case letter c to another file name nameC.txt.
The list of names to be put in the file are,
Adriana C. Ocampo Uria
Albert Einstein
Anna K. Behrensmeyer
Blaise Pascal Caroline Herschel
Cecilia Payne-Gaposchkin
Chien-Shiung Wu
Dorothy Hodgkin
Edmond Halley
Edwin Powell Hubble
Elizabeth Blackburn
Enrico Fermi
Erwin Schroedinger
Flossie Wong-Staal
Frieda Robscheit-Robbins
Geraldine Seydoux
Gertrude B. Elion
Ingrid Daubechies
Jacqueline K. Barton
Jane Goodall
Jocelyn Bell Burnell
Johannes Kepler
Lene Vestergaard Hau
Lise Meitner
Lord Kelvin
Maria Mitchell
Marie Curie
Max Born
Max Planck
Melissa Franklin
Michael Faraday
Mildred S. Dresselhaus
Nicolaus Copernicus
Niels Bohr
Patricia S. Goldman-Rakic
Patty Jo Watson
Polly Matzinger
Richard Phillips Feynman
Rita Levi-Montalcini
Rosalind Franklin
Ruzena Bajcsy
Sarah Boysen
Shannon W. Lucid
Shirley Ann Jackson
Sir Ernest Rutherford
Sir Isaac Newton
Stephen Hawking
Werner Karl Heisenberg
Wilhelm Conrad Roentgen
Wolfgang Ernst Pauli
!!!Do not store all names in an array.
Functions:
int getChoice() | This function should ask user to enter choices between 1 and 5, any other incorrect input should be casted and require a new input |
void handleMenu(ifstream & fin, ofstream & fout) | This function will repeat the menu as long as users choice is not a 5. This function should call all functions listed below, depends on the menu choice. |
void displayFirstFive(ifstream & fin) | This function should display the first five lines of the file. With a line number to each name |
void displayLastFive(ifstream & fin) | This function should display last five names of the file, with a line number. |
int count(ifstream & fin) | This function should count the number of names in the file. There shouldnt be any cout statements in this function. |
void copyToFile(ifstream & fin, ofstream & fout) | This function should copy all names contain a lower case letter c to a new file name nameC.txt. |
Hints:
Each time when you need to start from the top of the file, you should use the following two statements:
.clear();
.seekg(0, ios::beg);
One function should only handle one task. Feel free to add more functions if needed.
Given the following string variable, and I am trying to see if there might be a - in it. The following statements will help:
string s = "123-456";
if (s.find('-') != string::npos)
cout << "found!" << endl;
else
cout << "not found!" << endl;
To display the last five lines, you can use a counter to count till totalNumberOfLines 5, and then start displaying
The whole program should be less than 2 pages and the main function should be less than 15 lines with return 0 included.
Copy your nameC.txts content to your program as part of the output.
Sample Output:
********************************************* 1. Display first five names 2. Display last five names 3. Count the number of names 4. Copy Names to nameC.txt 5. Quit Enter your choice: 2 46: Sir Isaac Newton 47: Stephen Hawking 48: Werner Karl Heisenberg 49: Wilhelm Conrad Roentgen 50: Wolfgang Ernst Pauli
********************************************* 1. Display first five names 2. Display last five names 3. Count the number of names 4. Copy Names to nameC.txt 5. Quit Enter your choice: 3 There are 50 names
********************************************* 1. Display first five names 2. Display last five names 3. Count the number of names 4. Copy Names to nameC.txt 5. Quit Enter your choice: 1 1: Adriana C. Ocampo Uria 2: Albert Einstein 3: Anna K. Behrensmeyer 4: Blaise Pascal 5: Caroline Herschel
********************************************* 1. Display first five names 2. Display last five names 3. Count the number of names 4. Copy Names to nameC.txt 5. Quit Enter your choice: 4 Done! ********************************************* 1. Display first five names 2. Display last five names 3. Count the number of names 4. Copy Names to nameC.txt 5. Quit Enter your choice: 5 Over! |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started