C++ Problem with incrementing count when comparing two char arrays

This is a discussion on C++ Problem with incrementing count when comparing two char arrays within the C++ Programming forums, part of the General Programming Boards category; Hello All, This is my first post, so if I miss anything, please let me know... My project is to ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    28

    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...
    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"

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    Why pass in sizeTwo if you're not going to use it?!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing char arrays
    By Edelweiss in forum C Programming
    Replies: 6
    Last Post: 08-25-2011, 12:58 AM
  2. Uppercase/Lowercase/Word COunt/Char count
    By Charak in forum C Programming
    Replies: 7
    Last Post: 02-23-2011, 07:16 AM
  3. Comparing 2 char arrays
    By Rasher in forum C++ Programming
    Replies: 4
    Last Post: 02-25-2010, 12:48 PM
  4. incrementing a char pointer
    By Bleech in forum C Programming
    Replies: 13
    Last Post: 11-20-2006, 10:25 PM
  5. Comparing Character Arrays problem...
    By newy100 in forum C++ Programming
    Replies: 4
    Last Post: 11-16-2003, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21