help with number 6 c ++ 5. Redo Ex do Exercise 4 so that the customer can select
ID: 3731450 • Letter: H
Question
help with number 6 c ++
5. Redo Ex do Exercise 4 so that the customer can select multiple items of a particular vpe. A sample output in this case is: Welcome to Johnny's Restaurant 1 Bacon and Egg $2.45 2 Muffin $1.98 Coffee $0.50 Tax $0.25 Amount Due $5.18 Write a program whose main function is merely a collection of variable declarations and function calls. This program reads a text and outputs the letters, together with their counts, as explained below in the function printResult. (There can be no global variables! All information must be passed in and out of the functions. Use a structure to store the information.) Your program must consist of at least the following functions: Function openFile: Opens the input and output files. You must pass the file streams as parameters (by reference, of course). If the file does not exist, the program should print an appropriate message and exit. The program must ask the user for the names of the input and output files. Function count: Counts every occurrence of capital letters A-Z and small letters a-z in the text file opened in the function openFile. This information must go into an array of structures. The array must be passed as a parameter, and the file identifier must also be passed as a parameter.Explanation / Answer
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
struct info
{
char letter;
int count;
};
//function declaration
int openFile(ifstream &in, ofstream &out, string inFile, string outFile);
void Count(ifstream &in, info arr[], int n);
void printResults(ofstream &out, info arr[], int n);
int main()
{
ifstream in;
ofstream out;
string inFile, outFile;
//declare struct for representing 52 letters ie A-Z 26 chars and a-z 26 chars
info letterCount[52] = { 0 };
//ass user for input and output file
cout << "Enter input file name: ";
cin >> inFile;
cout << "Enter output file name: ";
cin >> outFile;
if (openFile(in, out, inFile, outFile) < 0)
{
cout << "Error opening input or output files.." << endl;
return -1;
}
Count(in, letterCount,52);
printResults(out, letterCount, 52);
in.close();
out.close();
}
int openFile(ifstream &in, ofstream &out,string inFile,string outFile)
{
in.open(inFile);
if (!in)
{
cout << "Not able to open " << inFile << endl;
return -1;
}
//open out put file
out.open(outFile);
if (!in)
{
cout << "Not able to open " << outFile << endl;
return -1;
}
return 0;
}
void Count(ifstream &in, info arr[], int n)
{
char ch;
while (!in.eof())
{
in >> ch;
if (ch >= 'A' && ch <= 'Z')
{
arr[ch - 65].letter = ch;
arr[ch - 65].count++;
}
if (ch >= 'a' && ch <= 'z')
{
arr[ch - 65].letter = ch;
arr[ch - 65].count++;
}
}
}
void printResults(ofstream &out, info arr[], int n)
{
//print info to file
for (int i = 0; i < n; i++)
{
if(arr[i].count>0)
out << arr[i].letter << " count : " << arr[i].count << endl;
}
}
------------------------------------------------------------------------------------------------------
/*output
//input file
You should NEVER use global variables. Other than the main() function, there should be no global functions in your code. Other than the test() function not other function needs to be a static function in this problem.
//input given to program
Enter input file name: LetteInput.txt
Enter output file name: LetteOut.txt
//content of output file
E count : 2
N count : 1
O count : 2
R count : 1
V count : 1
Y count : 1
a count : 9
b count : 6
c count : 7
d count : 4
e count : 16
f count : 5
g count : 2
h count : 11
i count : 11
l count : 8
m count : 2
n count : 18
o count : 17
p count : 1
r count : 7
s count : 9
t count : 20
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.