I am am supposed to prompt the user to enter the text to open the file. I have the code bold so you can find it better. The original name of the file are follows: outfile.open("textout.out"); infile.open("textin.txt"); how do I implement this ask the user to enter the file and open these?

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Stuck on this. I am am supposed to prompt the user to enter the text to open the file. I have the code bold so you can find it better. 

The original name of the file are follows:

outfile.open("textout.out"); 

infile.open("textin.txt");

how do I implement this ask the user to enter the file and open these?

 

//*************************************************************
// 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 <iomanip>
#include <cmath>
#include <fstream>
#include <cstring>
#include <string>
#include <iostream>


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;
string inputfile;
string outputfile;




cout << "Enter the input file name: ";
cin >> inputfile;
cout << endl;

infile.open(inputfile.c_str());



if (!infile) //Step 3
{
cout << "Cannot open the input file."
<< endl;
system("pause");
return 1;
}




cout << "Enter the output file name: ";
cin >> outputfile;
cout << endl;

outfile.open(outputfile.c_str());

//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<int>(ch)
- static_cast<int>('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<char>(index + static_cast<int>('A'))
<< " count = " << list[index] << endl;

system("pause");
} //end writeTotal

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY