I'm creating a class for a text analysis for letter frequency.
I am trying to compile it, but i'm having some problems. Could someone look at this for me?
This is what I have so far:

class Analysis
{
private:
	string letters[26];
	string text;
	int count;
	
public:
	Analysis()
    { 
		string letters[26] = {0};
		string text = 0;
		int count = 0;
		
    }	

Analysis (string l, string t, int c)
	{
		letters[26] = l;
		text = t;
		count = c;
		
	}
	
	void set(string l, string t, int c)
	{
		letters[26] = l;
		text = t;
		count = c;
	}
	
	string getLetters()
	{
		return letters;
	}
	
	string getText()
	{
		return text;
	}
	
	int getCount()
	{
		return count;
	}

void display_text() //displays text
	{
		int max;
		string text;
		cout << " Enter text here: " << endl;
		getline(cin, text);
		max = text.length();
}

int strfreq (string str, string subs) //count and return how many times substring appears in text
	{                 
		int ct = 0, pos=0;
		pos = str.find(subs, pos);             
		while (pos != -1)
		{
			ct++;                     
			pos = str.find(subs, pos + 1);         
		}
		return ct;
	}

int letterfreq ()
	{
		int letters[26];
		int count[26];
		
		for(int i = 0; i < 26; i++)
		{
			letters[i] = 0;
			count[i] = 0;
		}
}

};


int main()
{
	
	char ans;
	
	do 
	{
		Analysis wor;
		char ans;
		
		wor.display_text();
		cout << endl;
		
		wor.set(letters, text, count);
		
		wor.letterfreq ();
		
		
		cout << " Do you want to continue? (Y/N) " << endl;
		cin >> ans;
		cin.ignore();
		
	} while (toupper(ans) == 'Y');
	
	cout << setprecision(2) << fixed;
	
        for(i = 0; i < 26; i++)
		
	cout << setw(6) << (i + ('A')) << setw(6) << count[i] << setw(6) << letters[i] << endl;
	
	system("PAUSE");
	return 0;
}

Recommended Answers

All 7 Replies

All your constructors are declaring local variables which immediately go out of scope. Hence you may as well delete them. Even if they weren't going out of scope and you used the class variables, they are immediately going out of scope(can't use array index 26 - valid indexes are 0 to 25. Lines 70 and 71 not only have scope problems, but letters[] is declared as an int[] rather than a string[], so that's extra confusing.

But I'm wondering whether letters[] should be an int array anyway and line 4 is a mistake. letas no return statement. Line 96 uses a letters variable with no class object. Ditto line 111.

I think you need to start from scratch and decide what stores what and get rid of all of those useless local variables.

For the string errors, do one of these solutions:

#include <string>  //You need that or any include of a C++-standard include file to make std recognized.
using std::string;
//OR
using namespace std;
//OR
std::string blabla;  //For each string variable
I have corrected a few errors and it compiles well, but can't get the letters to count.
I need some serious feed back! I also included some comments below.
Could someone please please please look into this for me??


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iomanip>

using namespace std;

class Analysis
{
private:
int letters[26];
string text;
int count;

public:
Analysis()
{ 
text = "";


for(int x = 1; x <= 26; x++)
letters[x] = 0;	

}	

Analysis(int l, string t, int c)
{
letters[26] = l;
text = t;
count = c;

}

void set(int l, string t, int c)
{
letters[26] = l;
text = t;
count = c;
}

string getLetters()
{
return 0;
}

string getText()
{
return text;
}

int getCount()
{
return count;
}


void display_text() //displays text
	{
		
		int max;
		string text;
		cout << " Enter text here: " << endl;
		getline(cin, text);
		max = text.length();
		
		
	}
		
	int letterfreq () // letter frequency...something is wrong here. It isn't displaying the number of times letters come up...
//i'm also trying to total up the number of letters counted. How do I do that??
	{
		int letters[26];
		int count[26];
		string text;
		
		for(int x = 0; x < 26; x++)
		{
			letters[x] = 0;
			count[x] = 0;
		}
		
		
		int numb_of_letters = 0;
		int length = static_cast<int> (text.length());
		
		for(int x = 0; x < length; x++)
		{
			char lett = text[x];
			lett = toupper(lett);
			if('A' <= lett && lett <= 'Z')
			{
				int letter_x = lett - static_cast <int>('A');
				count[letter_x]++;
				numb_of_letters++;
			}
		}
		if(numb_of_letters > 0)
			for(int x = 0; x < 26; x++)
				letters[x] = static_cast<double> (count[x] / numb_of_letters);
		for(int x = 0; x < 26; x++)
		{
			cout << static_cast<char> (x + ('A')) << count[x] << letters[x] << endl;
		}
		return 0;
	}
		
	
			
		};
		
		
		
		int main() // i've placed a loop in the main. But the problem is that the user can't exit from the program. Whats up??
		{
			
			char ans;
			
			do 
			{
				Text words;
				
				
				words.display_text();
				cout << endl;
				
				words.letterfreq();
				
				cout << " Do you want to continue? (Y/N) " << endl;
				cin >> ans;
				cin.ignore();
				
			} while (toupper(ans) == 'Y');
			
			system("PAUSE");
			
			return 0;
		}

You can't post sentences inside code tags. As you can see, it comes out blue because the parser gets confused or something.

You also have tabs and spaces mixed up or it's not indented properly, so it's unreadable. Regardless, you have no class objects, so what's the point of having a class, and you still have all of those letters[] arrays declared all over the place. it's already declared on line 17. Don't declare it anywhere else.

Arrays in C++ always start at index 0, so valid indexes are 0 to 25, not 1 to 26. That's showing up all over the place.

Line 126 - What's Text?

IMHO the easiest way to count letter frequencies is to use an array of 255 elements -- which is large enough to count all possible characters. Then you can just use the letter itself as the index into the array.

Like this simple example: You would put this in a loop for each character

int counts[255] = {0};

counts['A']++; 

char c = 'B';
counts[c]++;

Now to output all the counts

for(int i = 0; i < 255; i++)
{
   if( counts[i] > 0)
     cout << "Letter " << (char)i << " is " << counts[i] << '\n';
}

hmmmm, I tried to change it according to your advice. But that didn't work. Are there any other tips? Thanks.

Post what you tried.

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.