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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
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.
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 227 228 229 230 231
|
#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 < 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(' ')<<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");
}
|
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