Write a program that reads in a text file named \"input.txt.\" As you read in th
ID: 3546327 • Letter: W
Question
Write a program that reads in a text file named "input.txt." As you read in this text file display its contents on the screen. Furthermore, you will maintain a count of how many times each letter appears in the text file. Your program should be case insensitive (meaning uppercase 'A' and lowercase 'a' should both be counted as one and the same.) Once your program has read the entire file it should display a SORTED frequency count of all letters. I can get it to count the letters but I get the incorrect letter for each count.
For example the CORRECT sample output should be
E = 165
T = 126
A = 102
but i keep getting
X = 102
Y = 126
Z = 165.
This is my code right now
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
void SortArray(int iArray[], int numElements);
void PrintArray(int total[], int i);
int main()
{
int total[26] = {0};
ifstream infile; //Declares variable to associate with file
string filename = "gettysburg.txt";
int index;
char c;
infile.open(filename); //Opens file
if (!infile.fail()) //Indicates if something went wrong while attempting to utilize file
{
while(!infile.eof()) //Indicates that end of file has been reached during input
{
while(infile.get(c)) //Reads each character one at a time
{
cout.put(c); //Prints each character to screen
if (isalpha(c)) //Checks for both upper and lower case Alphabetical characters
{
c = toupper (c); //Converts all alphabetical charactters to upper case
index = c - 'A'; //ASCII code for all uppercase characters is 65 - 90.
//This subtraccts 'A' to bring index to 0 thorugh 25.
total[index]++; //Total's index
}
}
}
cout << endl;
cout << endl;
cout << "Letter Frequency Table:" << endl;
cout << endl;
infile.close(); //Closes file
//Prints sorted array
SortArray(total, 26);
PrintArray(total, 0);
cout << endl;
}
else
{
cout << "Failed to open file '" << filename << "'!" << endl;
}
return 0;
}
void PrintArray(int total[], int i) //Prints Array
{
for(i = 0; i < 26; i++)
{
cout << char('A' + i) << " = " << total[i] << endl;
}
cout << endl;
}
void SortArray(int iArray[], int numElements) //Sorts Array
{
for(int curIndex = 0; curIndex <= numElements - 2; curIndex++)
{
int smallest = iArray[curIndex];
int smallestIndex = curIndex;
for(int checkIndex = curIndex + 1; checkIndex < numElements; checkIndex++)
{
if(iArray[checkIndex] < smallest)
{
smallest = iArray[checkIndex];
smallestIndex = checkIndex;
}
}
//Add here for swap
int temp;
temp = iArray[curIndex];
iArray[curIndex] = iArray[smallestIndex];
iArray[smallestIndex] = temp;
}
}
Explanation / Answer
Try this:#include<iostream> #include<fstream> #include<string>
using namespace std;
int main() { int letters[26]; // array to count letters int digits[10]; // array to count digits
//initialize arrays to zero for(int i = 0; i < 26; i++) letters[i] = 0; for(int i = 0; i < 10; i++) digits[i] = 0; ifstream file; string filename; string line; int len;
cout << "Enter the name of the .txt file to be analyzed : "; cin >> filename; cout << endl;
file.open(filename.c_str());
if (file.fail()) { cout << "Unable to find file " << endl; file.close(); return 1; }
while(!file.eof()) { getline(file, line); len = line.length(); if (len == 0) continue; // skip blank lines for(int i = 0; i < len; i++) { char c = line.at(i); if (c >= 65 && c <= 90) { letters[c - 65]++; } else if (c >= 97 && c <= 122) { letters[c - 97]++; } else if (c >= 48 && c <= 57) { digits[c - 48]++; } } }
file.close();
// print results; cout << endl;
for (char c = 'A'; c <= 'Z'; c++) { if (letters[c - 65] > 0) cout << c << " : " << letters[c - 65] << endl; }
for (char c = '0'; c <= '9'; c++) { if (digits[c - 48] > 0) cout << c << " : " << digits[c - 48] << endl; }
return 0; } Cheers TY
Note : Code developed in Visula c++ .. the code works fine Sample output ::: if file contains
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.