Just one word!

Pages: 123
I'm literally one word away from finishing this homework assignment. In my output it has to do ten words per line which it does but the last line has 6 words there are 7 lines before it consisting of 10 words so there is 76 words. When I count the input it has 75 words. My counter in my output says 75 words. This is literally a needle in a hay stack situation. I'm either counting wrong or my program is adding a word in the output. Something is off here.
This is the actual homework input file my program gets rid of all the crap in and around the words. So we are just counting words.


Tell friends that you have an eleven (11) fingers,
and will prove it! Touch each finger of left hand,
counting "One, two, three, fou4r, five".
Now count the fingers on your right hand,
"Six, seven, eight, nine, ten".
Say "Funny, Let's try again".
This time backwards, pointing to the fingers
of the left hand say "Ten, nine, eight, seven, six ...".
Stop, hold up the right hand,
say "Plus five equals eleven!".
Do this quickly, without pausing!!!


OK the function that I'm looking at is printTen I have been trying to see why this is the output

The file sorted alphabetically:
an and again backwards counting count Do each eight eight
equals eleven eleven fingers finger four five fingers friends Funny
fingers five hand hand have hand hold hand it left
Lets left Now nine nine One of of on pointing
Plus prove pausing quickly right right say Six seven six
Stop Say say seven Ten the two ten the
the the Tell try three that to Touch this time
This up without will you your

You see what I'm saying about 76 ( if I counted right)

This is my program
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

ofstream fout;

// Function to test for Empty file
void peekFile(ifstream &fin)
{	
	char empty;
	// To check if the file is empty
	empty=fin.peek();
	if(empty = fin.eof())
	fout << "No data exists" << endl;
}

// To Print Heading
void print_heading(ifstream &fin)
{
	//Formatting for Heading 
	fout << "Letter " << '\t' << "Count " << '\t'<< "Frequency " << endl;
	fout << setw(6) << setfill('=') << '='  <<'\t' << 
	setw(5) << setfill ('=') << '=' << '\t' << setw(9) 
	<< setfill('=') << '=' << endl;
	
	// Function incase file is empty
	peekFile(fin);
	

}

//Function to remove non - alpha
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 fill array of strings
void fillString(ifstream &fin, string inputarray[], const int size)
{
	// Loop to fill input array
	for(int i = 0; size > i; i++)
	{	
		fin >> inputarray[i];
	} 
}

// Function to sort array of strings
void selectionSort(string list[], int size)
{
	int i = 0, j = 0, smallest = 0; 
    string temp = "" ; string str1 = ""; string str2= "";
	
	for(i = 0; i < size -1; i++)
	{
		smallest = i;
		
		
	for(j = i + 1; j < size; j++)
	{  
		if (list[j] != "" && list[smallest] != "")
		{
			str1 = list[j];
			str2 = list[smallest];
			str1[0] = tolower(str1.at(0));
			str2[0] = tolower(str2.at(0));

			if( str1[0] < str2[0])
			{
				smallest = j;
			}
		}
	}
	    //swap
		temp = list[smallest];
		list[smallest] = list [i];
		list [i] = temp;
	}
}

//Function to fill array with only the words
void onlyWords(string str1[], const int isize, int& countWord)
{	
	//Loop to pass string at index i to onlyAlpha function
	for(int i = 0; i <  isize; i++)
	{
		onlyAlpha(str1[i]);
		if(str1[i].size()>0)
		countWord++;
	}		
}

//Function to fill frequency array
void  fillFrequency(string str1, int freqcount[], const int alphabet)
{		
	for(string::size_type i = 0; i < str1.size(); i++)
	{	
		str1[i] = toupper(str1[i]);
		++(freqcount[str1[i] - 'A']);
	}
}

//Function to display the highest frequency
int highestFreq(int freqcount[], const int alphabet)
{	
	int highestfreq = 0;
	
	for(int i = 0; i < alphabet; i ++)
	{
		if(freqcount[i] > highestfreq)
		{
			 highestfreq = freqcount[i];
		}
	}
		return highestfreq;
}

// Function to format and print 
void  printResults(int freqcount[], const int alphabet)
{	
	for (int i = 0; i < alphabet; i++)
	{
		if (freqcount[i] > 0)
		{
			fout <<' ' << char('A' + i) << setfill(' ')<<setw(9)
			<< freqcount[i] << setw(6);
			for (int j = 0; j < 2 * freqcount[i]; j++)
			{
				fout << '*';
			}
			fout << endl;
		}
	}
			fout << '\n' << endl;
}

//Function to print words ten to a line
void printTen(string inputarray[],const int size)
{
	fout << "The file sorted alphabetically: " << endl;
	int counter = 0;
	
	// This prints ten words per line
	for(int i = 0 ; i < size; i++)
	{	 
		if( counter % 10 != 0 || counter == 0)
		{
		 fout << inputarray[i] << ' ';
		
		}
		counter++ ;
		if(counter % 10 == 0 && counter != 0)
		{
		 fout << inputarray[i] << endl;
		}
	
	}	
}
	
		
void main()
{	
	ifstream fin;
	fin.open("mp6input.txt");
	fout.open("mp6output.txt");
	int wordCount = 0;
	int letterCount = 0;
	int highfreq = 0;
	const int size = 100;
	string inputarray[size];
	const int alphabet = 26;
	int  freqcount[alphabet] = {0};
	
	// to print heading
	print_heading(fin);
	
	// to fill string array
	fillString(fin, inputarray, size);
	
	//Only words function
	onlyWords(inputarray, size, wordCount);
	
	// To sort array 
	selectionSort(inputarray, size);
	
	// This loop gives words to fillfreqency function from inputarray
	for( string::size_type i = 0; i < size; i++)
	{
		fillFrequency(inputarray[i], freqcount, alphabet);
	}
	
	// Prints the table 
	  printResults(freqcount, alphabet);
	
	 //Loop to total letters
	for( int i = 0; i < alphabet ; i++)
	{
		letterCount += freqcount[i];
	}
	
	//Displays the number of words
	fout << "This file has " << wordCount << " words and "  
	<< letterCount <<" letters. " << endl;
	
	// To display the highest frequency
	highfreq = highestFreq(freqcount, alphabet);
	fout << "The highest frequency is " << highfreq 
	<< "." <<'\n' << endl;
	
	// Print the words 10 to a line
	printTen(inputarray, size);

	system("pause");

}


Thanks,
j
Try:
http://www.wordcounttool.com/

The ouput has one less word if you count "(11)" as a word in the input.
I got 76 for both input and output. I tred the characters on google I get all sorts of crap with some of the counters one but I did get 2 that said 400 for character count on my ouput this is not good.
thx
So if I do a cout statement in the only words function before my counter like this
1
2
3
4
5
6
7
8
9
10
11
12
13
//Function to fill array with only the words
void onlyWords(string str1[], const int isize, int& countWord)
{	
	//Loop to pass string at index i to onlyAlpha function
	for(int i = 0; i <  isize; i++)
	{
		onlyAlpha(str1[i]);
		if(str1[i].size() > 0)
		{  cout << str1[i] << "\t" << i <<"\t" << countWord << endl;
			countWord++;
		}		
	}
}


I get this output below. The rest of the program is the same as above. What I have found is that I'm missing the words " funny, time, pointing. There are a few words that are supposed to have 2 I'm supposed to have 2 "lefts, six" in the output. In the test of the output below I get the first off at eleven (number 6,6) and I get the second off at stop(61, 59)
I don't get this at all guys.
Any help please and thanks.











Tell 0 0
friends 1 1
that 2 2
you 3 3
have 4 4
an 5 5
eleven 6 6
fingers 8 7
and 9 8
will 10 9
prove 11 10
it 12 11
Touch 13 12
each 14 13
finger 15 14
of 16 15
left 17 16
hand 18 17
counting 19 18
One 20 19
two 21 20
three 22 21
four 23 22
five 24 23
Now 25 24
count 26 25
the 27 26
fingers 28 27
on 29 28
your 30 29
right 31 30
hand 32 31
Six 33 32
seven 34 33
eight 35 34
nine 36 35
ten 37 36
Say 38 37
Funny 39 38
Lets 40 39
try 41 40
again 42 41
This 43 42
time 44 43
backwards 45 44
pointing 46 45
to 47 46
the 48 47
fingers 49 48
of 50 49
the 51 50
left 52 51
hand 53 52
say 54 53
Ten 55 54
nine 56 55
eight 57 56
seven 58 57
six 59 58
Stop 61 59
hold 62 60
up 63 61
the 64 62
right 65 63
hand 66 64
say 67 65
Plus 68 66
five 69 67
equals 70 68
eleven 71 69
Do 72 70
this 73 71
quickly 74 72
without 75 73
pausing 76 74
OK so I don't think that the error is in the Only alpha function I made a litttle loop to see what was going on there I get the same as the what I posted above. So I don't think this is where its occuring .
I think that it has to do with the (11) in the input file above because of how it goes from 6 to 8 in the left colum of letters above but the count remains the same.
I got 76 for both input and output. I tred the characters on google I get all sorts of crap with some of the counters one but I did get 2 that said 400 for character count on my ouput this is not good.


There are 75 words as you are defining words. character count is not the same as the number of letters. Rather than trace the logic of your code, I slapped together a quick program that did the work and compared the results. Your word count and letter frequency as generated from the code in the original post is correct. (wc = 75, total letters = 321)

On the other hand, your sorting is obviously off.

Your output (emphasized words are out of order.):
The file sorted alphabetically: 
an and again backwards counting count Do  each eight eight
equals eleven eleven fingers finger four five fingers friends friends
fingers five hand hand have hand hold hand it it
Lets left Now nine nine One of of on on
Plus prove pausing quickly right right say Six seven seven
Stop Say say seven Ten the two ten the the
the the Tell try three that to Touch this this
This up without will you your   


My output:
The file sorted alphabetically:

again an and backwards count counting Do each eight eight 
eleven eleven equals finger fingers fingers fingers five five four 
friends Funny hand hand hand hand have hold it left 
left Lets nine nine Now of of on One pausing 
Plus pointing prove quickly right right say Say say seven 
seven six Six Stop Tell ten Ten that the the 
the the This this three time to Touch try two 
up will without you your 
Last edited on
Dude thanks. So you are saying that it is with my sorting algorithm or do you think that it is with my printing algorithm(printTen). I did 2 checks within the functions onlyAlpha and OnlyWords and those 2 functions both gave me 75 words those two functions have the same. So now I"m wonding what the h*ell is wrong with my sort. When I cout << the index (i) and the counter it becomes off by one as I stated above. How does this fit into this?
When I cout << the index (i) and the counter it becomes off by one as I stated above. How does this fit into this?


As you surmised, it's the (11). It's the only space-delimited token in the file that is not also (at least partly) a valid word, so it's the only 0-sized string in OnlyWords after it's been processed by OnlyAlpha. I have a suspicion it's also why you have the extra space in your alphabetically sorted list between Do and each, but that's just a hunch.

The printing is pretty straightforward. I doubt the issue is there.
Hmmm So are you sure that my sorting is off? Dude I think its that 11 it goes up to that then its off But It shouldn't be a problem. It should (should ha) skip over it. If it is not alpha then do not return it. At least thats what I want it to do.
If it is not alpha then do not return it.


If you're talking about your onlyAlpha function, it doesn't 'return' anything. It takes a reference to a string. It removes all non-alpha characters from that string, but the (empty) string still exists and onlyAlpha can't change that.

You could remove it in onlyWords, but that would add considerable complexity to your code, and won't solve the bigger problem.
So how am I supposed to take care of this? I meant return not in the sense of which it should be used in c++ but just saying that something is coming back( by reference)

Do you know what the problem is? Like I said all I got it that it's the 11. How come my sorting if off I don't understand that either I just have the example we did in class and it is pretty much the same except converting them to lower case.

In your sorting function, you are only comparing the first letter of each word. That's what causes the out of place sorting that cire pointed out.

Also, you skip the sorting step if the word happens to be blank. That's what leaves the extra space and causes one line to only have 9 words.
Hey let me through this idea out there. What if I had it return something and then just had an if statement that said if it is not alpha don't give it back. Then possibly I won't have that space because it won't be returned. Then I will try and do the sorting see what's wrong with that. What do you think ?

OK so I did this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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] );
	// Added
		if ((str1[i] >= 'A' && str1[i] <= 'Z') ||
			(str1[i] >= 'a' && str1[i] <= 'z'))
		str1 = newStr1;
	
	}
	
}


Now what happens is on the output like above where I display the i and the counter I get 76 letters because the (11) is present.
Ha. Why would this happen. Thanks
Your previous code:
1
2
3
4
5
6
7
8
9
10
11
12
13
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;
}


The newStr1 variable would have a value of "". And at the end, you set str1 = newStr1.

Your new code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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] );
	// Added
		if ((str1[i] >= 'A' && str1[i] <= 'Z') ||
			(str1[i] >= 'a' && str1[i] <= 'z'))
		str1 = newStr1;
	
	}
	
}


The newStr1 variable would still have a value of "". But at the end, you do not set str1 = newStr1, and so str1 stays with the value "(11)".

EDIT: Now that I think of it, what happened to the ...". ? It's probably in your output somewhere too, right?
Last edited on
OK so I did this


That would truncate all of your words to 1 letter.

It is equivalent to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 ;
            }
	}
}


Which, as you can see, assigns str1 the value of newStr1 after one character is added to newStr1, so that the strings are now equal and i will be greater or equal to str1.size() on the next iteration of the loop. Please don't change stuff randomly without thinking through the implications of the change.

As I said before, this is not an issue that you can address in onlyAlpha.
Last edited on
Yes it is just hanging out also the ...". So now that I know what the problem is how do I fix my Onlyalpha function. The problem is that "

(11)" and the ..." . I thought that my code handled that so that if would not be a problem now I'm confused what it is actually doing.
I don't know what you mean by this :


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Your previous code:

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;
}


The newStr1 variable would have a value of "". And at the end, you set str1 = newStr1.

I don't think it would because str1[i] comes back from isaplha function then
newStr1.push_back( str1[i] );
builds newStr1. Plus is that were the case how would I get any output when I was seeing if it worked. I would of just got a bunch of blank spaces because it would always because newStr1 woulld always be = "";
Maybe I don;t understand what your saying.
I meant when you passed the string "(11)" to the function, that's what happens.
cire (1529)

So how am I supposed to take care of this ???
Any ideas?
Pages: 123