Good Afternoon,

I'm having difficulties with a program that deal getting the letters frequencies from an input text file.
So far I have this code.

Cpp file:

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cmath> 
#include <cstdlib>
#include <iomanip>

#include "lab_19_head.h" 

using namespace std; 

int main( )
{
    //define a const array size
    const int SIZE = 26;                

    //array declaration
    int stats[SIZE];                        //letter stat array

    //file var
    ifstream inFile;                        //input file

    int i;                                  //loop var

    //open file
    inFile.open("lab_19_data.txt"); 
    if(!inFile)
    {
        cout<<"Fail to open lab_19_data.txt, and stop ."<<endl;
        return 1; 
    }

    //initialize array letterStata
    for (i=0; i<SIZE; i++)
        stats[i] = 0; 

    //call function get letter stats in the file
    getStats(stats, inFile, SIZE); 

    //close the file
    inFile.close(); 

    //call function showStats to display the results
    showStats(stats, SIZE); 



    //well done and exit
    return 0; 
}

Header file:

#include <iostream>
#include <cstring>

using namespace std; 

#ifndef     LAB_19_HEAD_H       
#define     LAB_19_HEAD_H



/****************************************************************
 This function gets letters frequencies from the input file.
 You need to implement this function.
 ****************************************************************/
void getStats(int stats[ ], ifstream & inFile, const int size)
{
    //Write your code here
    char ch;
    while(inFile)
    {
        inFile>>ch;
        ch = toupper (ch);
        if (ch >= 'A' && ch <= 'Z')
    }


}

/****************************************************************
 This function displays the stats inform in some good format.
 You need to implement this function.
 ****************************************************************/
void showStats(const int stats[ ], const int size)
{
    //Write your code here

    for (int i =0; i < size; i++)
    {
        cout << "The frequency of letter" << static_cast<char>(stats[ch - 'A'] && ['A' + i)
             << " is " <<stats[i]<<endl;
    }

}

#endif

Any help that I can get is appreciated it.
Thank you

if (ch >= 'A' && ch <= 'Z')

header file: you didn't finish the if statement.

Those two functions in that header file need to be moved to the *.cpp file becuse it will cause duplicate link errors if you include the header file in more than one *.c or *.cpp file. You can safely put function prototypes in header files, but not the entire function.

Ancient Dragon,
Here are the updated functions

void getStats(int stats[ ], ifstream & inFile, const int size)
{
    //Write your code here
    char ch;
    while(inFile)
    {
        inFile>>ch;
        ch = toupper (ch);
        if ('A' >= toupper(ch) && toupper(ch) <= 'Z')
        {
            stats[ch - 'A']++;
        }
    }


}

/****************************************************************
 This function displays the stats inform in some good format.
 You need to implement this function.
 ****************************************************************/
void showStats(const int stats[ ], const int size)
{
    //Write your code here
    char ch;
    for (int i =0; i < size; i++)
    {

        cout << "The frequency of letter" <<stats ['A' + i]<<"is"<<stats[i]<<endl;


    }

}

#endif

But, the program doesn't do what it suppose to do. Here is the output that I'm getting.

The frequency of letter0is0
The frequency of letter0is0
The frequency of letter3735168is0
The frequency of letter0is0
The frequency of letter-1is0
The frequency of letter2011722197is0
The frequency of letter1014646785is0
The frequency of letter0is0
The frequency of letter3735244is0
The frequency of letter2011471557is0
The frequency of letter3806189is0
The frequency of letter2130567168is0
The frequency of letter0is0
The frequency of letter0is0
The frequency of letter0is0
The frequency of letter0is0
The frequency of letter3806189is0
The frequency of letter2130567168is0
The frequency of letter0is0
The frequency of letter0is0
The frequency of letter0is0
The frequency of letter0is0
The frequency of letter0is0
The frequency of letter0is0
Press any key to continue . . .

cout << "The frequency of letter" <<stats ['A' + i]<<"is"<<stats[i]<<endl;

That line is incorrect -- there is no such element as stats['A'+i] because that's the same thing as stats[64+i]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.