C++ Problem with incrementing count when comparing two char arrays
Hello All,
This is my first post, so if I miss anything, please let me know...:cool:
My project is to take a string from the User, put it into a char array, compare it to a hardcoded array with the English alphabet, and then output to the screen how many times, if any, the letters from the string show up.
Example: User enters: abcdabcdef
The output should show: "A shows up 2 times." "B shows up 2 times." and so on until it encounters a letter from the alphabet not in the string, then output is: "Z shows up 0 times." So, the entire alphabet and its counts per letter have to be outputted.
****The problem is that I am not coming up with the structure to keep the variable "freqCount" accumulating. I've tried storing its value in another variable, e.g. "totalFreq = totalFreq +freqCount" and it isn't changing the value of "totalFreq".
****If you look at the output, you'll see that the program IS counting the occurence of each letter correctly up to the size of the "buffArray" which is 10, however, it isn't making a ongoing tally of the counts. Any advise is greatly appreciated!
--------------------------------------------------
****START OF OUPUT:******
Enter the string up to 11 letters.
abcdabcdef
Here's are the contents of the buffer now: abcdabcdef
This is buffer array: abcdabcdef
This is alphabet array: abcdefghijklmnopqrstuvwxyz
It's a match. a shows up 1 times.
It's a match. b shows up 1 times.
It's a match. c shows up 1 times.
It's a match. d shows up 1 times.
The letter e shows up 0 times.
The letter f shows up 0 times.
The letter g shows up 0 times.
The letter h shows up 0 times.
The letter i shows up 0 times.
The letter j shows up 0 times.
It's a match. a shows up 1 times.
It's a match. b shows up 1 times.
It's a match. c shows up 1 times.
It's a match. d shows up 1 times.
It's a match. e shows up 1 times.
It's a match. f shows up 1 times.
The letter g shows up 0 times.
The letter h shows up 0 times.
The letter i shows up 0 times.
The letter j shows up 0 times.
Press any key to continue . . .
****END OF OUTPUT*****
--------------------------------------------------------------
I am now pasting my code for Visual Studio or if you use DevCPP, just remove #include "stdafx.h" and #include <array> from the header area.
---------------------------------------------------------------
Code:
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <array>
#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, 26);
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;
// start loop for user input now in "buffArray"
for(int i = 0; i < (sizeOne -1); )
{
buffArray[i];
// start loop for precoded alphabet array in "alphaArray"
for(int j = 0; j < (sizeOne - 1); )
{
alphaArray[j];
// compare each char in the user array element by element to the alphabet array, and if it's a match, make "countFreq" = 1.
// next go to the second element in "buffArray" and see if it's a match to the first element in "alphaArray". If it is, increase the count by another -1-.
if(alphaArray[j] == buffArray[i])
{
countFreq = 1;
i++; // go to the next element in "buffArray"
cout << "It's a match. " << buffArray[i] << " shows up " << countFreq << " times." << endl;
}
else
{
cout << "The letter " << alphaArray[j] << " shows up 0 times." << endl;
}
j++; // after looping through "buffArray" go to _
// next element in "alphaArray" and compare _
// again
}
}
} // end "parseBuffer"