#include <fstream>
#include <iostream>
usingnamespace std;
int main()
{
int freq[128]; // frequencies of letters
ifstream inFile; // input file
char ch;
inFile.open("firstyear.txt");
if (!inFile)
{
cout << "The input file could not be opened.";
return 1;
}
// initialize frequency counts to zero for each possible letter
for (int k = 0; k < 128; k++)
{
freq[k] = 0;
}
// Read the file, keeping track of frequency with which each letter occurs
ch = inFile.get();
while (ch != EOF)
{
cout << ch;
ch = toupper(ch);
freq[ch]++;
ch = inFile.get();
}
// Print the output table
cout << endl << "Letter frequencies in this file are as follows." << endl;
for (char ch = 'A'; ch <= 'Z'; ch++)
{
cout << ch << " : " << freq[ch] << endl;
}
return 0;
}
then your program's working directory (which is probably the directory the executable is in, and probably not the directory the code is in) does not contain the file.
#include <fstream>
#include <iostream>
usingnamespace std;
int main()
{
int freq[128]; // frequencies of letters
fstream inFile; // input file
string buffer;
char ch;
inFile.open("joke.txt", ios::out);
if (!inFile)
{
cout << "The input file could not be opened."<<endl;
return 1;
}
// initialize frequency counts to zero for each possible letter
for (int k = 0; k < 128; k++)
{
freq[k] = 0;
}
// Read the file, keeping track of frequency with which each letter occurs
ch = inFile.get();
while (ch != EOF)
{
cout << ch;
ch = toupper(ch);
freq[ch]++;
ch = inFile.get();
}
// Print the output table
cout << endl << "Letter frequencies in this file are as follows." << endl;
for (char ch = 'A'; ch <= 'Z'; ch++)
{
cout << ch << " : " << freq[ch] << endl;
}
return 0;
}
// Author: Aaron
// main.cpp
// test
//
// Created at Home on 2/23/12.
//
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <stdio.h>
usingnamespace std;
// Function to increment the letter count in the counters array
char add_letter( char letter, int counters[] ) {
// Only deal with lower case letters
char lower_case_letter = tolower(letter);
// Check if character is a letter
if ( lower_case_letter >= 'a' && lower_case_letter <= 'z' ) {
++counters[ lower_case_letter - 'a' ];
}
return letter;
}
int main() {
// One integer for each letter
int counters[26] = { 0 };
// String which is inputed word
string word;
// Prompts user for word and gets word in the word variable
cout << "Enter a Word: ";
getline(cin,word);
// Converts string word into const char which is needed for the function
constchar *sentence = word.c_str();
// Pass each character to the add_letter
// function until null-terminator is reached
while ( add_letter(*sentence++, counters) );
// Display results
for (int i = 0; i < 26; ++i) {
if ( counters[i] ) {
cout << char(i + 'a') << ": " << counters[i] << '\n';
}
}
// Prompts user for ENTER to end program
cout << "hit ENTER to end program...";
cin.ignore();
return 0;
}
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
int freq[128]; // frequencies of letters
ifstream inFile; // input file
char ch;
inFile.open("kentucky.txt");
if (!inFile)
{
cout << "The input file could not be opened."<<endl;
return 1;
}
// initialize frequency counts to zero for each possible letter
for (int k = 0; k < 128; k++)
{
freq[k] = 0;
}
// Read the file, keeping track of frequency with which each letter occurs
ch = inFile.get();
while (ch != EOF)
{
cout << ch;
ch = toupper(ch);
freq[ch]++;
ch = inFile.get();
}
// Print the output table
cout << endl << "Letter frequencies in this file are as follows." << endl;
for (char ch = 'A'; ch <= 'Z'; ch++)
{
cout << ch << " : " << freq[ch] << endl;
}
return 0;
}