Question about adding integers to char

This is a discussion on Question about adding integers to char within the C++ Programming forums, part of the General Programming Boards category; I'm working on a problem that counts the frequency of letters. I basically have the logic correct (it's from my ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    8

    Question Question about adding integers to char

    I'm working on a problem that counts the frequency of letters. I basically have the logic correct (it's from my textbook so I'm going to assume it should run ok. However, when it goes to print out the frequency, the numbers are way too high (in the millions).

    Here's the code:

    Code:
    #include <iostream>
    using namespace std;
    int main(){
    char ch;
    int freqLetter[200];
    int LineCounter = 0;
    for(ch = 'a'; ch<= 'z'; ch++ ){
            freqLetter[ch] = 0; // init array
            cout<<"Enter some lower case letters (CTRL-Z to end):";
            while(cin>>ch){
                freqLetter[ch]= freqLetter[ch]+1;
                }
                cout<<"\n\n\n\t\tLetter Frequency "<<endl<<endl;
    
                for(ch = 'a'; ch <= 'z'; ch++){
                    cout<<" "<<ch<<" = "<<freqLetter[ch];
                    if(LineCounter >= 5){
                        cout<<endl;
                        LineCounter = 0;} //end if
                    LineCounter++;} // end for
                cout<<endl;
                return 0;
        }
    }
    I'm assuming the code given is correct in the fact that incrementing a char will procede to the next letter (incrementing a gives b, incrementing b gives c).
    Thanks for the help!

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Think about the values ch takes in the course of your program. Hint: you're using it for several things at once.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,753
    You did not initialise all the relevant counts to 0 before accessing the array arbitrarily.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    that's because you should end your first for-loop wraps your entire code, instead of just the zeroing of the freqletter. So you get "rubbish" (or possibly some initialized value to show that you are using uninitialized variables).

    Code:
    int freqLetter[200] = { 0 };
    would be a much shorter way to initialize your array.

    You probably should also check that your read character is in the range 'a' .. 'z' before accessing the array freqLetter[ch] - if you are unlucky, it will be a negative value (since char can be either signed or unsigned (up to the compiler which it is), and anything that has the topmost bit of a signed char will be treated as negative. So for exampel would be a negative character in this case - and that is DEFINITELY outside your array!].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    Quote Originally Posted by CornedBee View Post
    Think about the values ch takes in the course of your program. Hint: you're using it for several things at once.
    Isn't the first for loop setting the count of each element to 0? That is what you guys mean correct?
    Thanks for the super quick reply!

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,753
    Quote Originally Posted by blueshift1980
    Isn't the first for loop setting the count of each element to 0? That is what you guys mean correct?
    Yes, at least for the elements with indices 'a' to 'z', but the problem is that you do not just access those initialised elements on each iteration of the loop; you may also access those that will only be initialised on a later iteration. This suggests that you need to rethink the structure of what you implemented.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    Quote Originally Posted by matsp View Post
    that's because you should end your first for-loop wraps your entire code, instead of just the zeroing of the freqletter. So you get "rubbish" (or possibly some initialized value to show that you are using uninitialized variables).

    Code:
    int freqLetter[200] = { 0 };
    would be a much shorter way to initialize your array.

    You probably should also check that your read character is in the range 'a' .. 'z' before accessing the array freqLetter[ch] - if you are unlucky, it will be a negative value (since char can be either signed or unsigned (up to the compiler which it is), and anything that has the topmost bit of a signed char will be treated as negative. So for exampel would be a negative character in this case - and that is DEFINITELY outside your array!].

    --
    Mats
    Thanks matsp! I get it now. I knew not to trust what was in the book! Thanks for the quick reply, I will so be back to this forum constantly for help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char ** and argv memory question
    By simo_mon in forum C Programming
    Replies: 3
    Last Post: 08-17-2008, 08:47 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  3. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  4. AnsiString versus char *
    By Zahl in forum C++ Programming
    Replies: 35
    Last Post: 10-16-2002, 08:38 PM
  5. what does this warningmean???
    By kreyes in forum C Programming
    Replies: 5
    Last Post: 03-04-2002, 06:53 AM

Tags for this Thread


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