I need help with my C++ homework and im stuck. The problem is to write a program that reads a file of all lowercase and output each letter and how many times each letter occurs.... Here is the code i have and where i am stuck....

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#inlcude <fstream>
using namespace std;


int main()
{
    ofstream infile;
    ifstream outfile;
    int count [0] = {0};
    char letter [26] = {' '};
    char next;
    int next_location;

    infile.open("inputfile.txt");

    if(in_file.fail())
    {
                      cout << "Input file did not open please fix. \n";
                      system("pause");
                      exit(1);
                      }






infile >> next;

for(

Edited 3 Years Ago by mike_2000_17: Fixed formatting

You're overwhelming yourself. Start by reading the file and printing out each character, one-by-one. Break the problem down into smaller problems that are easier to tackle individually.

Little successes are important, and having something simple that works is much better for keeping your spirits up while working on a problem than something complex that doesn't work. It all snowballs from there. :)

But the problem is to use 2 arrays and to store the letters in one array and the occurances in the other array. Ive already declared them i jus need help using them

Does the assignment tell you to use 2-arrays? Using an array to store every letter in the file is a little difficult to do unless you know how many letters there are to begin with.

You don't have to store them to output them. You can just use a temp, process the temp for counting, output it, then repeat.

begin loop:
  read a char from the file
  find out what letter you have read
  increment the appropriate counter
  output the letter
end loop
output counts

Edited 6 Years Ago by Fbody: n/a

Make an array of int count[26], initialize them all to 0. Read in the first character. Type cast the character to an int, subtract 97 from the int. That value is now the index of your count array.

If the letter is 'a', it will typecast to the value 97, 97-97=0. count[0] is your a value, so count[0]++. If the letter is 'b', typecast it will be 98. 98-97=1 so count[1]++.

The other way is to have a massive switch statement.

Make an array of int count[26], initialize them all to 0. Read in the first character. Type cast the character to an int, subtract 97 from the int. That value is now the index of your count array.

If the letter is 'a', it will typecast to the value 97, 97-97=0. count[0] is your a value, so count[0]++. If the letter is 'b', typecast it will be 98. 98-97=1 so count[1]++.

The other way is to have a massive switch statement.

Or you could subtract the characters directly:

char readChar = '\0';
int temp = 0;
infile.get(readChar);
temp = readChar - 'a';
counterArray[temp]++;

Edited 6 Years Ago by Fbody: n/a

This article has been dead for over six months. Start a new discussion instead.