Okay so I need help with this c++ code to change it to where it asks the user to enter the file and search. I am going to break the instructions in three parts so you can understand. First, its the book instructions. Then, the what the actual file looks like and the original code. Book instructions First I want you to modify the program to prompt the user for the name of the file to output. The file that is used in the code Today we live in an era where information is processed almost at the speed of light. Through computers, the technological revolution is drastically changing the way we live and communicate with one another. Terms such as “the Internet,” which was unfamiliar just a few years ago, are very common today. With the help of computers you can send letters to, and receive letters from, loved ones within seconds. You no longer need to send a résumé by mail to apply for a job; in many cases you can simply submit your job application via the Internet. You can watch how stocks perform in real time, and instantly buy and sell them. Students regularly “surf” the Internet and use computers to design their classroom projects. They also use powerful word processing software to complete their term papers. Many people maintain and balance their checkbooks on computers. The actual code that needs to be changed //************************************************************* // Author: D.S. Malik // // Program: Line and Letter Count // This program reads a text, outputs the text as is, and also // prints the number of lines and the number of times each // letter appears in the text. An uppercase letter and a // lowercase letter are treated as being the same; that is, // they are tallied together. //************************************************************* #include #include #include using namespace std; void initialize(int& lc, int list[]); void characterCount(char ch, int list[]); void copyText(ifstream& intext, ofstream& outtext, char& ch, int list[]); void writeTotal(ofstream& outtext, int lc, int list[]); int main() { //Step 1; Declare variables int lineCount; int letterCount[26]; char ch; ifstream infile; ofstream outfile; infile.open("textin.txt"); //Step 2 if (!infile) //Step 3 { cout << "Cannot open the input file." << endl; return 1; } outfile.open("textout.out"); //Step 4 initialize(lineCount, letterCount); //Step 5 infile.get(ch); //Step 6 while (infile) //Step 7 { copyText(infile, outfile, ch, letterCount); //Step 7.1 lineCount++; //Step 7.2 infile.get(ch); //Step 7.3 } writeTotal(outfile, lineCount, letterCount); //Step 8 infile.close(); //Step 9 outfile.close(); //Step 9 return 0; } void initialize(int& lc, int list[]) { int j; lc = 0; for (j = 0; j < 26; j++) list[j] = 0; } //end initialize void characterCount(char ch, int list[]) { int index; ch = toupper(ch); //Step a index = static_cast(ch) - static_cast('A'); //Step b if (0 <= index && index < 26) //Step c list[index]++; } //end characterCount void copyText(ifstream& intext, ofstream& outtext, char& ch, int list[]) { while (ch != '\n') //process the entire line { outtext << ch; //output the character characterCount(ch, list); //call the function //character count intext.get(ch); //read the next character } outtext << ch; //output the newline character } //end copyText void writeTotal(ofstream& outtext, int lc, int list[]) { int index; outtext << endl << endl; outtext << "The number of lines = " << lc << endl; for (index = 0; index < 26; index++) outtext << static_cast(index + static_cast('A')) << " count = " << list[index] << endl; } //end writeTotal
Okay so I need help with this c++ code to change it to where it asks the user to enter the file and search. I am going to break the instructions in three parts so you can understand. First, its the book instructions. Then, the what the actual file looks like and the original code.
Book instructions
First I want you to modify the program to prompt the user for the name of the file to output.
The file that is used in the code
Today we live in an era where information is processed
almost at the speed of light. Through computers, the
technological revolution is drastically changing the way we
live and communicate with one another. Terms such as
“the Internet,” which was unfamiliar just a few years ago, are
very common today. With the help of computers you can send
letters to, and receive letters from, loved ones within
seconds. You no longer need to send a résumé by mail to apply for a job; in many cases you can simply submit your job
application via the Internet. You can watch how stocks perform in real time, and instantly buy and sell them. Students
regularly “surf” the Internet and use computers to design
their classroom projects. They also use powerful word
processing software to complete their term papers. Many
people maintain and balance their checkbooks on computers.
The actual code that needs to be changed
//*************************************************************
// Author: D.S. Malik
//
// Program: Line and Letter Count
// This program reads a text, outputs the text as is, and also
// prints the number of lines and the number of times each
// letter appears in the text. An uppercase letter and a
// lowercase letter are treated as being the same; that is,
// they are tallied together.
//*************************************************************
#include
#include
#include
using namespace std;
void initialize(int& lc, int list[]);
void characterCount(char ch, int list[]);
void copyText(ifstream& intext, ofstream& outtext, char& ch,
int list[]);
void writeTotal(ofstream& outtext, int lc, int list[]);
int main()
{
//Step 1; Declare variables
int lineCount;
int letterCount[26];
char ch;
ifstream infile;
ofstream outfile;
infile.open("textin.txt"); //Step 2
if (!infile) //Step 3
{
cout << "Cannot open the input file."
<< endl;
return 1;
}
outfile.open("textout.out"); //Step 4
initialize(lineCount, letterCount); //Step 5
infile.get(ch); //Step 6
while (infile) //Step 7
{
copyText(infile, outfile, ch, letterCount); //Step 7.1
lineCount++; //Step 7.2
infile.get(ch); //Step 7.3
}
writeTotal(outfile, lineCount, letterCount); //Step 8
infile.close(); //Step 9
outfile.close(); //Step 9
return 0;
}
void initialize(int& lc, int list[])
{
int j;
lc = 0;
for (j = 0; j < 26; j++)
list[j] = 0;
} //end initialize
void characterCount(char ch, int list[])
{
int index;
ch = toupper(ch); //Step a
index = static_cast(ch)
- static_cast('A'); //Step b
if (0 <= index && index < 26) //Step c
list[index]++;
} //end characterCount
void copyText(ifstream& intext, ofstream& outtext, char& ch,
int list[])
{
while (ch != '\n') //process the entire line
{
outtext << ch; //output the character
characterCount(ch, list); //call the function
//character count
intext.get(ch); //read the next character
}
outtext << ch; //output the newline character
} //end copyText
void writeTotal(ofstream& outtext, int lc, int list[])
{
int index;
outtext << endl << endl;
outtext << "The number of lines = " << lc << endl;
for (index = 0; index < 26; index++)
outtext << static_cast(index + static_cast('A'))
<< " count = " << list[index] << endl;
} //end writeTotal
Trending now
This is a popular solution!
Step by step
Solved in 2 steps