Letter Frequenices

How come I keep getting a file error?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <fstream>
#include <iostream>
using namespace 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;
}
If by file error you mean you keep getting
"The input file could not be opened."
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.
so i have to create the file and then open it?
Does the file firstyear.txt actually exist? Your code runs fine on a file that does exist and is in the same directory as the compiled program.
Last edited on
But i thought
inFile.open("firstyear.txt");
would open the file and let it run. If not what am i supposed to change?
You're supposed to have the file firstyear.txt in the same directory as the executable. Alternatively, give the complete path:

inFile.open("~/home/kdm/firstyear.txt");
or perhaps
inFile.open("C:/some/directory/firstyear.txt");
depending on your OS.

The file must exist if you are to open it and read its contents.
Last edited on
Sorry I'm still confused, I tried it like this and it still didn't work

inFile.open(C:/Users/Jimmy/Documents/"firstyear.txt");
Last edited on
I changed some stuff and now all i get is zeros

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <fstream>
#include <iostream>
using namespace 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;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//  Author: Aaron 
//  main.cpp
//  test
//
//  Created at Home on 2/23/12.
//

#include <iostream>
#include <fstream>
#include <cstdlib> 
#include <string> 
#include <stdio.h>

using namespace 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 
    const char *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;
}

this worked for me.
thanks, I got this to work for me

#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;
}
Topic archived. No new replies allowed.