# Thread: weird response when counting the letters.

1. ## 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. 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. 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. 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. 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. 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. 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.

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