Hello guys,
I wanna write a program that count for me the number of each letter I have in a text file then I want to cout the words and count the number of repetition of each word.

I was able to do it except for counting the repetition

And here is my code:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdlib>
#include <cstring>
#include <stdio>
#include <conio>

int main()
{
char ch;                   int num[100]={0};      string read; string k[10];
string q[10], news; int index=0,indecies=0;
const int arraysize=27;
    char a[arraysize]={'q','w','e','r','t','y','u','i','o','p','a','s','d','f',
    'g','h','j','k','l','z','x','c','v','b','n','m',' '};
    int freq[arraysize]={0};
ifstream fin("mah.txt");
if (!fin.good())
	{
	cout << "Cannot find your location" << endl;

	}

	while ((ch = fin.get()) != EOF) {
     for(int j=0;j<=arraysize;j++)
      {
       if (ch==a[j])
       freq[j]=freq[j]+1;
      }
       if (ch==' ')
       {
       index++;
       for(int j=0;j<=index-2;j++)
         if(q[j]==q[index-1])
         num[j]++;
         for(int i=0;i<=index;i++)
   		if(q[i]!=' ')
         k[i]=q[i];

       }
       else

       q[index].append(ch);
                               }
  	cout << endl;
	fin.close();
   for(int j=0;j<arraysize;j++)
   cout<<setw(2)<<(j+1)<<"  the freq of "<<a[j]<<" is "<<freq[j]<<".\n";
   for(int i=0;i<=index;i++)
   cout<<k[i]<<endl;




	getch ();
	return 0;
}

Waiting for you help

So what's the problem with your code?

Did this actually compile for you? If so, what results does it give?

It looks like you're doing too much spinning in the loop to find the match to characters, but it works, I think.

What is the code at lines 42-44 below supposed to be doing? Comparing strings to individual char's is not gonna work.

Lines 39-41 have some loop control issues, and while it looks like a way to count repeated words, you won't know how many distinct words occurred. Do you have 4 repeats of the same word, or 4 pairs of words that occurred? And when did you put any words into the q-array?

You really should work on formatting, some indenting helps readability a lot, like this (note: this DOES NOT COMPILE - you've got some fixing to do)

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <conio.h>
using namespace std;

int main()
{
    char ch; int num[100]={0}; 
    string read; 
    string k[10];
    string q[10], news; 
    int index=0,indecies=0;
    const int arraysize=27;
    char a[arraysize]={'q','w','e','r','t','y','u','i','o','p','a',
                              's','d','f', 'g','h','j','k','l','z','x',
                              'c','v','b','n','m',' '};
    int freq[arraysize]={0};
    ifstream fin("mah.txt");
    if (!fin.good())
    {
        cout << "Cannot find your location" << endl;

    }

    while ((ch = fin.get()) != EOF) {
        for(int j=0;j<=arraysize;j++)
        {
            if (ch==a[j])
                freq[j]=freq[j]+1;
        }
        if (ch==' ')
        {
            index++;
            for(int j=0;j<=index-2;j++)
                if(q[j]==q[index-1])
                    num[j]++;
            for(int i=0;i<=index;i++)
                if(q[i]!=' ')
                    k[i]=q[i];

        }
        else

            q[index].append(ch);
    }
    cout << endl;
    fin.close();
    for(int j=0;j<arraysize;j++)
        cout<<setw(2)<<(j+1)<<" the freq of "<<a[j]<<" is "<<freq[j]<<".\n";
    for(int i=0;i<=index;i++)
        cout<<k[i]<<endl;

    getch ();
    return 0;
}

i want to calculate the the repetition of each word.

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.