weird response when counting the letters.

This is a discussion on weird response when counting the letters. within the C++ Programming forums, part of the General Programming Boards category; I have to count the letters that were inputted by the user. This a sample of my code and the ...

  1. #1
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154

    Question weird response when counting the letters.

    I have to count the letters that were inputted by the user. This a sample of my code and the answer in the console. (a the varibles are previously defined).
    Code:
         cout << " text:";
         cin >> message;
         
         let = 0;
         
         // analysis part of the program //
         while(let < message_length)
         {
                   switch((int)message[let])
                   {
                   case 65: a++; break;
                   case 66: b++; break;
                   case 67: c++; break;
                   case 68: d++; break;
                   case 69: e++; break;
                   case 70: f++; break;
                   case 71: g++; break;
                   case 72: h++; break;
                   case 73: i++; break;
                   case 74: j++; break;
                   case 75: k++; break;
                   case 76: l++; break;
                   case 77: m++; break;
                   case 78: n++; break;
                   case 79: o++; break;
                   case 80: p++; break;
                   case 81: q++; break;
                   case 82: r++; break;
                   case 83: s++; break;
                   case 84: t++; break;
                   case 85: u++; break;
                   case 86: v++; break;
                   case 87: w++; break;
                   case 88: x++; break;
                   case 89: y++; break;
                   case 90: z++; break;
                   }
                   
                   let = every_so_many_letters + let;
         }
    
    // Print the answer to screen //
    
         cout << "\ta:\t" << a << endl;
         cout << "\tb:\t" << b << endl;
         cout << "\tc:\t" << c << endl;
         cout << "\td:\t" << d << endl;
         cout << "\te:\t" << e << endl;
         cout << "\tf:\t" << f << endl;
         cout << "\tg:\t" << g << endl;
         cout << "\th:\t" << h << endl;
         cout << "\ti:\t" << i << endl;
         cout << "\tj:\t" << j << endl;
         cout << "\tk:\t" << k << endl;
         cout << "\tl:\t" << l << endl;
         cout << "\tm:\t" << m << endl;
         cout << "\tn:\t" << n << endl;
         cout << "\to:\t" << o << endl;
         cout << "\tp:\t" << p << endl;
         cout << "\tq:\t" << q << endl;
         cout << "\tr:\t" << r << endl;
         cout << "\ts:\t" << s << endl;
         cout << "\tt:\t" << t << endl;
         cout << "\tu:\t" << u << endl;
         cout << "\tv:\t" << v << endl;
         cout << "\tw:\t" << w << endl;
         cout << "\tx:\t" << x << endl;
         cout << "\ty:\t" << y << endl;
         cout << "\tz:\t" << z << endl;
    the response:
    Code:
    a:      2088809675
    b:      16384
    c:      4008936
    d:      2293440
    e:      4335876
    f:      2293560
    g:      4408300
    h:      4209872
    i:      1
    j:      2293424
    k:      4404400
    l:      2293552
    m:      2
    n:      0
    o:      16777216
    p:      -1
    q:      4424736
    r:      -1
    s:      0
    t:      0
    u:      1
    v:      4
    w:      2293452
    x:      8
    y:      4008955
    z:      2293488
    No matter what text you put in and however long the char message ; part is, the same result comes and I don't get it. Hope some can help.

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    I can't find a problem in the code you posted, so I assume the poblem is elswhere. My best guess is that either a control variable like message_length wasn't initialized or you overran the area of memory you actually intended to scan.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    New Zealand
    Posts
    20
    you can do that code is a much much easier way than that by using an array for the letters. I suspect your problem is that you haven't initialised your variables (a, b, c, ...) to zero.

  4. #4
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154
    I found what was causing the problem, but don't understand why. It works fine when the integers a,b,c,d...z are all initialised globally instead of locally. Why, i have no idea

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    New Zealand
    Posts
    20
    Are you initilising them in the function they're being used in? With out all of the code one can never really tell whats going on.

  6. #6
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154
    No, the only way i can get it to work is by starting them outside of a "function block". The example shows what i mean.
    Code:
    int 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;
    void function();
    
    using namespace std;
    
    void function()
    {
    // This is where the code from eariler goes. If the varibles are started 
    // within this data block, i get the weird response.
    }
    by the way, I set them to zero before the while loop.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Global variables are automatically initialized to zero. Local variables, unless defined as static, are not. You were using your variables uninitialized. Now you know better, so in the future you'll be sure and initialize all of your variables before you use them.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Mar 2005
    Location
    New Zealand
    Posts
    20
    Quote Originally Posted by Finchie_88
    No, the only way i can get it to work is by starting them outside of a "function block". The example shows what i mean.
    Code:
    int 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;
    void function();
    
    using namespace std;
    
    void function()
    {
    // This is where the code from eariler goes. If the varibles are started 
    // within this data block, i get the weird response.
    }
    by the way, I set them to zero before the while loop.
    That was the bit I wanted to know. Here is a much simpler way of doing what yu want:
    Code:
    void function()
    {
      int num_of_letters[26];
      int let;
    
      for(let=0; let<26; let++)
        num_of_letters[let]=0;
    
      for(let=0; let<message_length; let++)
        if(toupper(message[let])>='A' && toupper(message[let])<='Z')
          num_of_letters[toupper(message[let])-'A']++;
    
      for(let=0; let<26; let++)
        cout << "\t" << (char) let << ":\t" << message[let] << endl;
    }

  9. #9
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    Just a sugestion, you could use a array of int:

    Code:
    int count[256];
    
    for(int i=0;i<message_len;i++)
       count[message[i]]++;
    Sorry if it was desnecessary, I just wanted to give a cleaner way of solving the problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. counting letters in string
    By CGbiginner in forum C Programming
    Replies: 1
    Last Post: 03-31-2008, 11:47 AM
  2. Counting uppercase and lowercase letters in a text
    By Tintu in forum C Programming
    Replies: 2
    Last Post: 02-06-2008, 09:15 PM
  3. Counting letters and digits
    By FeNCinGeR in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2006, 11:39 AM
  4. Help with counting letters
    By hpy_gilmore8 in forum C++ Programming
    Replies: 3
    Last Post: 10-24-2003, 03:49 PM
  5. I need help counting letters., folk!
    By correlcj in forum C Programming
    Replies: 7
    Last Post: 07-14-2002, 12:31 PM

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