Help please. I wrote a code, but is such a mess I have to start from scratch. If
ID: 3775742 • Letter: H
Question
Help please. I wrote a code, but is such a mess I have to start from scratch. If you are able to put together a working code maybe I can see where I am messing up.
use VOID functions:
Write a code that will prompt the user for an input file, prompt the user for an output file name, process the input file and output your results to an output file.
your output file should look like this:
* Name: YOUR NEAME GOES HERE
* Input File Name : p3in1.txt
* Output File Name: outPut_p3in1.txt
* Number of lines = 7
* Number of chars = 269
* Five Most Used Characters
'E' 33 counted
'N' 26 counted
'T' 25 counted
'A' 24 counted
'R' 21 counted
* Characters Not Used: J, Q, Z,
________________________________________
Character Occurence
- - - - - - - - - - - -
A count = 24 B count = 2 C count = 5
D count = 8 E count = 33 F count = 7
G count = 4 H count = 10 I count = 19
J count = 0 K count = 2 L count = 18
M count = 6 N count = 26 O count = 18
P count = 5 Q count = 0 R count = 21
S count = 11 T count = 25 U count = 13
V count = 1 W count = 2 X count = 2
Y count = 7 Z count = 0
****** input file example: *********
NOTE: The file must have a line return after the last character, in order for our example code to run properly. If you don’t have that blank line at the end of the input file, the program will not perform as intended and behave unpredictably.
---------------Input.txt begins---------------
9 8 7 6 5 You must have a line return after the
last character in your message, in order
for our example code to run properly. Note
that there are 8 lines of text in this file
including an additional mandatory "blank line"...otherwise
your system may hang up and create huge output files
the intentional line return and final blank line follows
Explanation / Answer
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void updateCounts(int counts[], string line){
int ind = 0;
char ch;
for(int i = 0; i < line.size(); ++i){
ch = line[i];
if(ch >= 'a' && ch <= 'z')
ch -= 32;
if(ch >= 'A' && ch <= 'Z'){
ind = ch - 'A';
++counts[i];
}
}
}
void printHeader(ofstream &out, string name, string inFile, string outFile){
out << "* Name: " << name << endl;
out << "* Input File Name : " << inFile << endl;
out << "* Output File Name: " << outFile << endl;
}
void printStats(ofstream &out, int counts[], int numChars, int numLines){
out << "* Number of lines = " << numLines << endl;
out << "* Number of chars = " << numChars << endl;
out << "* Five Most Used Characters" << endl;
out << "* Characters Not Used: " << endl;
for(int i = 0; i < 26; ++i){
if(counts[i] == 0)
out << (char)('A' + i) << " ";
}
out << endl;
}
void printResult(ofstream &out, int counts[]){
out << "________________________________________" << endl;
out << "Character Occurence" << endl;
out << "- - - - - - - - - - - -" << endl;
for(int i = 0; i < 26; ++i){
cout << (char)('A' + i) << " count = " << counts[i] << " ";
if((i + 1) % 3 == 0)
cout << endl;
}
}
int main(){
string fileName, line, inFile;
int counts[26], numChars = 0, numLines = 0;
cout << "Enter input file name: ";
cin >> fileName;
inFile = fileName;
ifstream in(fileName.c_str());
if(in.is_open()){
cout << "Enter output file name: ";
cin >> fileName;
ofstream out(fileName.c_str());
for(int i = 0; i < 26; ++i){
counts[i] = 0;
}
while(getline(in, line)){
updateCounts(counts, line);
numChars += line.size() + 1;
++numLines;
}
printHeader(out, "NAME", inFile, fileName);
printStats(out, counts, numChars, numLines);
printResult(out, counts);
in.close();
out.close();
}
else{
cout << "can not open " << fileName << " to read" << endl;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.