I just need some help to debug this program. I thought I had it done and I'm having problems with my output. The issue is that my words contain 75 words and when I output them in alphabetical order I get 10 per line and then the last line has 6. I have no clue why my program is off. Someone told me it was my sorting function which my teacher actually gave to use in lecture. So I'm really at my ends about what the actual issue is. Someone said that my sorting was off and they got

 ``1234567891011121314151617181920212223`` ``````My 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 Their 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 ``````

This is what myinput is
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!!!

My whole program is supposed to take this and just get the words (get rid of the junk) Then its supposed to tell the frequency and such and how many words, letters and such.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231`` ``````#include #include #include #include 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 < str2) { 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) { // This cout the i and word count are out of sync I think it has to do with the 11 in the input file because it is out of wack at 6 //cout << str1[i] << "\t" << i <<"\t" << countWord << endl; 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(' ')<

This is my entire program if you compile it you will see.
If you uncomment the statement above in onlyWords it will show that the i and the count are out of sync. I'm so lost where the issues are in this thing. Thought I was done ha :)

Thanks to whoever looks I appreciate it
You really have 77 words. 2 of them are invalid "(11)" and "..."." hence you count 75.

The invalid words are not removed from the array and they are actually printed. Notice the gap between "Do each" on the first line. You print for some reason 11 words there. In fact you print all 100 words. I don't know what happens to the second invalid word. That's a subject for further investigations...
So, How do I pin point this problem. That's really the issue. One person suggested my sort algorithm was off. I thought possibly my print function of printTen because of the 11 space thing. The words should of never been put back into the array if I this had went to plan that is what the only Alpha function was supposed to take care of. How do you know I"m printing all 100? I don't know how you can tell that.
THanks:)
First of all you should stick to your nomenclature. `print_heading()` is different to all other... But that's not the problem

 How do you know I"m printing all 100? I don't know how you can tell that.
Now you're funny. Whenever you use `size` in `main()` it is 100

The root of your failure is `fillString()`. Better name it `readStrings()`. I give you an example how it's supposed to look:
 ``1234567891011121314151617181920212223242526272829303132333435`` ``````int readStrings(ifstream &fin, string inputarray[], const int size) { int result_size = 0; // Loop to fill input array for(int i = 0; fin.good() && (i < size); i++) // EDIT: good is better ;) { string str; fin >> str; onlyAlpha(str); if(fin.good() && (!str.empty())) // The trick: do not insert what you don't want to be inserted! { inputarray[result_size] = str; ++result_size; } } return result_size; } ... int main() { ... const int overall_size = 100; string inputarray[overall_size]; ... const int size = readStrings(fin, inputarray, overall_size); // to fill string array fillString(fin, inputarray, size); //Only words function onlyWords(inputarray, size, wordCount); ... return 0; }``````

 One person suggested my sort algorithm was off
No, I wouldn't say so. Funnily you choose the selection sort which is somewhat advance compared to the bubble sort. But that ok.
Last edited on
Wel I didn't choose it my instructor actually went over it and on my hw it said use a copy of the algorithm from the book. I wasn;t trying to be funny. I meant that the size was still 100 ok I know that but I thought that I wasn't putting the strings I didn't want back in the array so even though size was 100 still maybe there are only 75 words that is what I was refering to as size.
if you have to use `onlyWords()` I can show you how to easily remove the unwanted words. But really: you should use `wordCount` for follow up processing and not `size`
Yeah it worked man. That user vlad from moscow told me very similar things to what you were saying. I appreciate it so much you have no idea 2 days looking at this no clue how to fix it. I just got rid of the onlyWords function and used wordCount as suggested. Now the only thing is I get 11 words on the first line which is supposed to be 10. See I was so confused because I thought that my Onlywords function was doing what you had suggested for me in the fillString function.

Dude thanks!
 Now the only thing is I get 11 words on the first line which is supposed to be 10.
For that problem I'd say roughly:
 ``123456`` `````` for(int i = 0 ; i < size; i++) { if( (i % 10) == 0 ) fout << endl; fout << inputarray[i] << ' '; }``````
Last edited on
I got this

 ``12345678910111213141516171819202122`` ``````/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; } } } ``````
So I'm still having issues. This code makes 11 in the first line.

 ``12345678910111213141516171819`` ``````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] << ' '; } else if(counter % 10 == 0 && counter != 0) { fout << inputarray[i] << endl; } counter++ ; } } ``````

Does anyone think that this may not be an issue with the printTen function. I just don't get why It would print 11 on the first line.
 Does anyone think that this may not be an issue with the printTen function. I just don't get why It would print 11 on the first line.
That's not too hard to see. the first line is the only line where `counter == 0`. That's the reason why you have one more word. I don't know why you're using `counter`. It has no other value than `i`

if you don't want an end line on the first line, try this:
 ``123456`` `````` for(int i = 0 ; i < size; i++) { if(( (i % 10) == 0 ) && (i > 0)) fout << endl; fout << inputarray[i] << ' '; }``````
That didn't really fix it dude. I got a different output totally.
I got this
The file sorted alphabetically:

eleven
friends
left
Plus
seven
the
up

OH lord ha
gOT IT THANKS@!!!!
Topic archived. No new replies allowed.