Hello all, I am working on a practice textbook question that asks a user to input 10 characters that go into an array and then that array is compared to a hard-coded alphabet array. The output should display the number of duplicates, if any, per letter. For example:
"There are 2 a's."
"There are 0 b's."
"There are 3 c's." .....and so on.
My code is correctly counting the number of duplicates (or not) between the 2 arrays. However, the problem is that it is displaying the count EVERY TIME THE LOOP ITERATES. I only need it to display THE TOTAL COUNT.
I tried moving the "cout" statement below the loop which doesn't work because it needs the [i] and [j] from where it loops thru the arrays.
Please point out where my error is, thanks in advance!
Code:#include <iostream> #include <iomanip> #include <string> #include <algorithm> #include <Windows.h> using namespace std; void parseBuffer( char buffArray[], char alphaArray[], int sizeOne, int sizeTwo ); int main() { // precode alphabet into an array with null terminating character char alphabetArray[]={'a','b','c','d','e','f','g','h','i','j','k','l','m',n', 'o','p','q','r','s','t','u','v','w','x','y','z','\0'}; char buffer[11]; cout << "Enter the string up to 10 letters." << endl; cin.get(buffer, 11); parseBuffer(buffer, alphabetArray, 11, 11); system("Pause"); return 0; } void parseBuffer(char buffArray[], char alphaArray[], int sizeOne, int sizeTwo) { int countFreq = 0; cout << "This is buffer array: " << buffArray << endl; cout << "This is alphabet array: " << alphaArray << endl<< endl; for(int i = 0; i < (sizeTwo - 1); i++) { alphaArray[i]; for(int j = 0; j < (sizeOne -1); j++) { buffArray[j]; if(alphaArray[i] == buffArray[j] ) { countFreq = countFreq + 1; } else { countFreq = 0; } cout << "It's a match. " << alphaArray[i] << " shows up " << countFreq << " times." << endl << endl; } } } // end "parseBuffer"