USING C++, You will create a text file called “names.txt”, which contains a list
ID: 3804161 • Letter: U
Question
USING C++,
You will create a text file called “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 names (just copy and paste these) to be put into the “names.txt” 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 user’s 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 shouldn’t 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.txt’s content to your program as part of the output.
IT IS IMPERATIVE THAT YOUR OUTPUT FOLLOWS THE SAMPLE OUTPUT BELOW (INCLUDING THE *’S). PLEASE INCLUDE A SCREENSHOT OF YOUR OUTPUT OR COMMENT OUT YOUR OUTPUT IN YOUR CODE SO I CAN SEE THAT IT IS IDENTICAL TO THE SAMPLE OUTPUT BELOW. PLEASE KEEP THE HINTS ABOVE IN MIND WHEN WRITING THE PROGRAM
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!
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 user’s 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 shouldn’t 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”.
Explanation / Answer
#include <bits/stdc++.h>
using namespace std;
int getChoice()
{
int ch;
cout<<"1. Display first five names 2. Display last five names 3. Count the number of names 4. Copy Names to nameC.txt 5. Quit";
cout<<"Enter your choice ";
cin>>ch;
return ch;
}
void displayFirstFive(ifstream & fin)
{
int c=0;
string n;
while(c<5)
{
getline(fin,n);
cout<<c+1<<":"<<n<<endl;
c++;
}
}
int count(ifstream & fin)
{
int c=0;
string n;
while(getline(fin,n))
{
c++;
}
return c;
}
void copyToFile(ifstream & fin, ofstream & fout)
{
string n;
while(getline(fin,n))
{
if (n.find("c") != std::string::npos)
{
fout<<n<<endl;
}
}
}
void displayLastFive(ifstream & fin)
{
int c=49;
string n;
int t=0;
while(t<c)
{
getline(fin,n);
if(t>=c-5)
cout<<t+2<<":"<<n<<endl;
t++;
}
}
void handleMenu(ifstream & fin, ofstream & fout)
{
int ch=getChoice();
switch(ch)
{
case 1:displayFirstFive(fin);break;
case 2:displayLastFive(fin);break;
case 3:count(fin);break;
case 4:copyToFile(fin, fout);break;
case 5:exit(0);break;
}
}
int main(int argc, char const *argv[])
{
ifstream fi("names.txt");
ofstream oi("nameC.txt");
handleMenu(fi,oi);
return 0;
}
=======================================================
Output:
akshay@akshay-Inspiron-3537:~/Chegg$ g++ name.cpp
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
1. Display first five names
2. Display last five names
3. Count the number of names
4. Copy Names to nameC.txt
5. QuitEnter your choice
2
46:Sir Isaac Newton
47:Stephen Hawking
48:Werner Karl Heisenberg
49:Wilhelm Conrad Roentgen
50:Wolfgang Ernst Pauli
akshay@akshay-Inspiron-3537:~/Chegg$ g++ name.cpp
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
1. Display first five names
2. Display last five names
3. Count the number of names
4. Copy Names to nameC.txt
5. QuitEnter your choice
1
1:Adriana C. Ocampo Uria
2:Albert Einstein
3:Anna K. Behrensmeyer
4:Blaise Pascal Caroline Herschel
5:Cecilia Payne-Gaposchkin
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
1. Display first five names
2. Display last five names
3. Count the number of names
4. Copy Names to nameC.txt
5. QuitEnter your choice
2
46:Sir Isaac Newton
47:Stephen Hawking
48:Werner Karl Heisenberg
49:Wilhelm Conrad Roentgen
50:Wolfgang Ernst Pauli
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
1. Display first five names
2. Display last five names
3. Count the number of names
4. Copy Names to nameC.txt
5. QuitEnter your choice
3
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
1. Display first five names
2. Display last five names
3. Count the number of names
4. Copy Names to nameC.txt
5. QuitEnter your choice
5
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.