Stuck still

I'm still so stuck on this problem. I keep getting the same error. I have looked at it for a long time now and no progress.
error C2664: 'freqHelp' : cannot convert parameter 1 from 'char' to 'std::string &'
When I read the error I think that this is a mix up of the types. OK, so then I got to where the error is and I'm so stuck still.
I'm trying to take an array of of integers 1-26 from the alphabet. I'm going to give the array to a function. This function within my function to take the array will then go through each word. It will convert the word to uppercase then it will -65. So when it gets stored in the array that is of type int with size 26 the position 0 will correspond to A and it will be incremented one every time a letter is stored ( at that postion that is). OK hopefully you guys understand me I have tried hard to explain my purpose hopefully looking at the code will connect this better for you.
Thanks,
j

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include<iomanip>
#include <string>
using namespace std;

//Function to remove garbage off word
void onlyAlpha(string& str1)
{	
	string newStr1 ="";
	newStr1.reserve(str1.size()); 

	for(string::size_type i = 0; i < str1.size(); i++)
	{	
		if ( isalpha( str1[i] ) ) 
		newStr1.push_back( str1[i] );
	}
	str1= newStr1;
}

// to help frequency process
 void freqHelp(string& str1)
 {
	 for(string::size_type i =0; i < str1.size(); i++)
	 {
		 toupper(str1[i]);
		 str1[i-65] ++;
	 }
}

 //A function to determine frequency of letters
void  fillFrequency(string str1, int freqcount[], const int alphabet)
{	
	for(string::size_type i = 0; i <str1.size(); i++)
	{
			 freqHelp(str1[i]);
	}
}


//Function to fill new array with new words.
void funfun(string str1[], const int isize, int& countWord)
{	

	for(int i = 0; i <  isize; i++)
	{
		onlyAlpha(str1[i]);
		countWord++;
	}		
}


void main()
{
	const int isize = 3;	
	string str1[] = {"!!HELLO","WHAT###", "%%NOW"};
	string test = "!!dude";
	const int size = 100;
	string wordsminus[size];
	int wordCount = 0;
	const int alphabet = 26;
	int freqcount[alphabet];
	funfun(str1,isize,wordCount);
	onlyAlpha(test);
	
	fillFrequency(test, freqcount, alphabet);
	
	for(int j = 0; j < isize; j++)
	{
		cout << str1[j]<< "\t";
	}
	cout << wordCount;
	system("pause");
}	



The error is so confusing because to me I don't know where the char is coming from I have an array of int and I have strings that is it.
freqHelp(str1[i]);
Here is you trying to call the function freqHelp. The function freqHelp accepts a string object.

What kind of object is str1[i]? It's a char.

So you're trying to pass a char to something that wants a string.
Moreover this code

1
2
3
4
5
6
7
8
void freqHelp(string& str1)
 {
	 for(string::size_type i =0; i < str1.size(); i++)
	 {
		 toupper(str1[i]);
		 str1[i-65] ++;
	 }
}


is invalid in whole. You are trying to use index i - 65 that is beyond acceptable indexes for the string.
Last edited on
Moscow, I thought it was OK. I convert the index at i to upper so now if Subtract 65 then it places it between one and 26 which I was trying to store in an array of size 26 for the alphabet. The i++ will increment it for example 65-65 =0 ++ is 1 so it can be stored in an array and pos 1 is incremented by one.
Whats wrong with that idea? I don;t know how that is invalid in the code. At least the above it the idea I was shooting for.
I am sorry but I do not know what is "convert the index at i to upper".:) Do you know that i has type string::size_type? Can you explain how to convert to upper size_type?
1
2
3
4
5
6
7
8
9
10
11
12
13
//Function to remove garbage off word
void onlyAlpha(string& str1)
{	
	string newStr1 ="";
	newStr1.reserve(str1.size()); 

	for(string::size_type i = 0; i < str1.size(); i++)
	{	
		if ( isalpha( str1[i] ) ) 
		newStr1.push_back( str1[i] );
	}
	str1= newStr1;
}


Were you aware that this will reverse the string? You need push_front instead.

Also I don't see where you populate the freqcount array.

I can see you are trying to use the integer value of the chars in the alphabet to calculate a position in the freqcount array. You need separate statements for this.

Also, the value return by the function toupper needs to be assigned to something.

HTH
No its fine dude . OK
1
2
3
4
5
6
7
8
void freqHelp(string& str1)
 {
	 for(string::size_type i =0; i < str1.size(); i++)
	 {
		 toupper(str1[i]);
		 str1[i-65] ++;
	 }
}


OK so this part what my brain is trying to do is to take a word str1 use []
to mean the same thing as str1.at(). So I call that str1[i]. I just used I so when the loop goes it will increment the position one by one getting all the letters from the word converting them to upper case. I think that toupper() converts the letter at position i to uppercase. So 'a' becomes 'A'. I was trying to make that happen with this statement. Then the str1[i-65]++, was just a attempt to take i which is now uppercase and subtract 65 and add one so I can store it in an array at what ever position that is. For instance 65-65=0 then ++ makes it one so when it is put into an array of chars it will store it in position i. Thats what I shooting for or trying to accomplish.
Thx hopefully that explains it
So really my whole goal is to take words from arrays go through the words and have another array which corresponds to the frequency of the words. That's my goal.
As Vlad said, the index is illegal. When i == 0, you have str1[-65] - which is obviously illegal.

To calculate the value of a char:

position = str[i] -65;

Now you can use the position variable to place the frequency in the freqcount array.

freqcount[position] = ;//the frequency of that char

What I was saying about toupper - you need to assign it to something:

str1[i] = toupper(str1[i]); //convert to uppercase

So really my whole goal is to take words from arrays go through the words and have another array which corresponds to the frequency of the words. That's my goal.


That contradicts what you said earlier. Are you counting the frequency of chars or words?
Last edited on
Even better:

position = str[i] -'A';
OK thanks I will try out your "idea" TheIdeasMan (1235). For the contradiction
ignoring the c++ lingo and just saying it in english. I just want to take a word and count each letter. So the word " apple" has 1 a, 2 p's, 1 l, and 1 e. So take p for instance I just want to count that and store that number in an array. I said it this way so hopefully I won't confuse myself if I try and explain it in c++ lingo. But back to c++ ha. I will take that count of 2 and have an array of 26 and that each letter will go to its proper place in the alphabet.
I really hope that I made it clear. I'm going to try that idea above though. Maybe I can work with that. I know what I want to do the problem for me always is expressing that properly in c++. What a battle thanks for the help dude.
I will try it after a bit see how it goes.
Hi I tried to do something like suggested above. I"m getting the craziest output ever! It compiles but I get some sort of block like thing. I'm supposed to get numbers. I can't even describe this compile it you will see wtf!

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include<iomanip>
#include <string>
using namespace std;

//Function to remove garbage off word
void onlyAlpha(string& str1)
{	
	string newStr1 ="";
	newStr1.reserve(str1.size()); 

	for(string::size_type i = 0; i < str1.size(); i++)
	{	
		if ( isalpha( str1[i] ) ) 
		newStr1.push_back( str1[i] );
	}
	str1= newStr1;
}


 //A function to determine frequency of letters
void  fillFrequency(string str1, char freqcount[], const int alphabet)
{	
	int position;
	for(string::size_type i = 0; i <str1.size(); i++)
	{
			 i = toupper(str1[i]);
			 position = i- 65;
		for( int j = 0; j < alphabet; j++)
		{
			freqcount[i] = position ++;
		}

	}
}


//Function to fill new array with new words.
void funfun(string str1[], const int isize, int& countWord)
{	

	for(int i = 0; i <  isize; i++)
	{
		onlyAlpha(str1[i]);
		countWord++;
	}		
}


void main()
{
	const int isize = 3;	
	string str1[] = {"!!HELLO","WHAT###", "%%NOW"};
	string test = "!!dude";
	const int size = 100;
	string wordsminus[size];
	int wordCount = 0;
	const int alphabet = 26;
	char freqcount[alphabet];
	funfun(str1,isize,wordCount);
	onlyAlpha(test);
	
	fillFrequency(test, freqcount, alphabet);
	
	for(int i = 0; i < isize; i++)
	{
		cout << str1[i]<< "\t";
	}
	cout << wordCount << endl;
	
	for(int j = 0; j < alphabet; j++)
	{
		cout << freqcount[j] << "\t";
	}
	
	
	
	
	
	
	
	system("pause");
}
Opps I noticed this but now that it is changed I get a bunch of char characters


i = toupper(str1[i]);
position = i- 65;
for( int j = 0; j < alphabet; j++)
{
freqcount[i] = position ++;
}

} I just changed the i to j
Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//A function to determine frequency of letters
void  fillFrequency(string str1, char freqcount[], const int alphabet)
{	
	int position;
	for(string::size_type i = 0; i <str1.size(); i++)
	{
			 i = toupper(str1[i]); //don't reuse the loop variable
                         position = toupper(str1[i]) - 'A'; //better to use the char rather than a number
			 position = i- 'A';    //combined these together in line above
		for( int j = 0; j < alphabet; j++) //no need to cycle through alphabet
		{
			(freqcount[position])++; //parentheses for precedence
		}

	}
}

Topic archived. No new replies allowed.