Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ I need to open a text file with the name of the text file entered by the use

ID: 3877114 • Letter: C

Question

C++

I need to open a text file with the name of the text file entered by the user, read each line seperately, then read each character in the line tally how many times the the character appears, then write the character and count of how many times the character appears in the line to an output file. Then read the next line in the text file and write the characters & count to a different output file, reapeat this process until all lines of the text file are read. I also need to make sure the letters whether 'A' or 'a' are counted as 'a'. I believe I need to use arrays, getline and ifstream and ofstream.  

Explanation / Answer

// Code

/*C++
I need to open a text file with the name of the text file entered by the user, read each line seperately, then read each character in the line tally how many times the the character appears, then write the character and count of how many times the character appears in the line to an output file. Then read the next line in the text file and write the characters & count to a different output file, reapeat this process until all lines of the text file are read. I also need to make sure the letters whether 'A' or 'a' are counted as 'a'. I believe I need to use arrays, getline and ifstream and ofstream.

*/

#include <iostream>
#include <fstream>
using namespace std;

int main() {
  
// taking user input for a file
string eachline, fname;
cout << "Enter the file name: ";
cin >> fname;
  
// opening file
ifstream inputFile (fname);
  
if (inputFile.is_open())
{
// to generate files with names having suffix as line number
int countLines = 1;
  
// as long as there are lines in file
while ( getline (inputFile,eachline) )
{
// taking an array for requency
int freq[26] = {0};
  
// looping through each character in line
for(int ind=0; ind<eachline.length(); ind++)
{
// if character is an alphabet incrementing it's frequency by 1
if(eachline[ind] >= 'a' && eachline[ind] <= 'z' || eachline[ind] >= 'A' && eachline[ind] <= 'Z' )
{
if(eachline[ind] >= 'a' && eachline[ind] <= 'z')
freq[eachline[ind] - 97]++;
if( eachline[ind] >= 'A' && eachline[ind] <= 'Z')
freq[eachline[ind] - 65]++;
}
}
  
// taking a file to write to
ofstream outFile;
outFile.open ("out"+to_string(countLines)+".txt");
  
// writing frequency of each letter
for(int i=0; i<26; i++)
{
outFile << (char)(i+65) << "-" <<freq[i]<<" ";
}
  
// incrementing the count of lines by 1
countLines++;
}
inputFile.close();
}
}

/* Sample output is given here

input.txt

This is different text to
correct having aaa aaa AAA
BBB ccc DDD eee

out1.txt

A-0
B-0
C-0
D-1
E-3
F-2
G-0
H-1
I-3
J-0
K-0
L-0
M-0
N-1
O-1
P-0
Q-0
R-1
S-2
T-5
U-0
V-0
W-0
X-1
Y-0
Z-0

out2.txt

A-10
B-0
C-2
D-0
E-1
F-0
G-1
H-1
I-1
J-0
K-0
L-0
M-0
N-1
O-1
P-0
Q-0
R-2
S-0
T-1
U-0
V-1
W-0
X-0
Y-0
Z-0

out2.txt

A-0
B-3
C-3
D-3
E-3
F-0
G-0
H-0
I-0
J-0
K-0
L-0
M-0
N-0
O-0
P-0
Q-0
R-0
S-0
T-0
U-0
V-0
W-0
X-0
Y-0
Z-0

*/

// Code

/*C++
I need to open a text file with the name of the text file entered by the user, read each line seperately, then read each character in the line tally how many times the the character appears, then write the character and count of how many times the character appears in the line to an output file. Then read the next line in the text file and write the characters & count to a different output file, reapeat this process until all lines of the text file are read. I also need to make sure the letters whether 'A' or 'a' are counted as 'a'. I believe I need to use arrays, getline and ifstream and ofstream.

*/

#include <iostream>
#include <fstream>
using namespace std;

int main() {
  
// taking user input for a file
string eachline, fname;
cout << "Enter the file name: ";
cin >> fname;
  
// opening file
ifstream inputFile (fname);
  
if (inputFile.is_open())
{
// to generate files with names having suffix as line number
int countLines = 1;
  
// as long as there are lines in file
while ( getline (inputFile,eachline) )
{
// taking an array for requency
int freq[26] = {0};
  
// looping through each character in line
for(int ind=0; ind<eachline.length(); ind++)
{
// if character is an alphabet incrementing it's frequency by 1
if(eachline[ind] >= 'a' && eachline[ind] <= 'z' || eachline[ind] >= 'A' && eachline[ind] <= 'Z' )
{
if(eachline[ind] >= 'a' && eachline[ind] <= 'z')
freq[eachline[ind] - 97]++;
if( eachline[ind] >= 'A' && eachline[ind] <= 'Z')
freq[eachline[ind] - 65]++;
}
}
  
// taking a file to write to
ofstream outFile;
outFile.open ("out"+to_string(countLines)+".txt");
  
// writing frequency of each letter
for(int i=0; i<26; i++)
{
outFile << (char)(i+65) << "-" <<freq[i]<<" ";
}
  
// incrementing the count of lines by 1
countLines++;
}
inputFile.close();
}
}

/* Sample output is given here

input.txt

This is different text to
correct having aaa aaa AAA
BBB ccc DDD eee

out1.txt

A-0
B-0
C-0
D-1
E-3
F-2
G-0
H-1
I-3
J-0
K-0
L-0
M-0
N-1
O-1
P-0
Q-0
R-1
S-2
T-5
U-0
V-0
W-0
X-1
Y-0
Z-0

out2.txt

A-10
B-0
C-2
D-0
E-1
F-0
G-1
H-1
I-1
J-0
K-0
L-0
M-0
N-1
O-1
P-0
Q-0
R-2
S-0
T-1
U-0
V-1
W-0
X-0
Y-0
Z-0

out2.txt

A-0
B-3
C-3
D-3
E-3
F-0
G-0
H-0
I-0
J-0
K-0
L-0
M-0
N-0
O-0
P-0
Q-0
R-0
S-0
T-0
U-0
V-0
W-0
X-0
Y-0
Z-0

*/