Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
how come i can't get the corret value for the element..??
i wrote aloha in the txt file but when it print out the elemnet inside the char array
i get unmatching value..??
pls help!!
#include <iostream>
#include <fstream>using namespace std;
class histogram
{
public:
int OpenText();
char Table();
private:
protected:
char str1[40];
int str2[25];
int total,i,j,k;
};int histogram::OpenText()
{
fstream open("Text.txt",ios::in);
while (!open.eof())
{
open.getline(str1,41);
cout << str1 << endl;
}
for(j=0;j<26;j++)
{
str2[j]=0;
}
for(i=0;i<41;i++)
{
if(str1[i]=='a' || str1[i]=='A')
{
str2[0]++;
}
else if(str1[i]=='b' || str1[i]=='B')
{
str2[1]++;
}
else if(str1[i]=='c' || str1[i]=='C')
{
str2[2]++;
}
else if(str1[i]=='d' || str1[i]=='D')
{
str2[3]++;
}
else if(str1[i]=='e' || str1[i]=='E')
{
str2[4]++;
}
else if(str1[i]=='f' || str1[i]=='F')
{
str2[5]++;
}
else if(str1[i]=='g' || str1[i]=='G')
{
str2[6]++;
}
else if(str1[i]=='h' || str1[i]=='H')
{
str2[7]++;
}
else if(str1[i]=='i' || str1[i]=='I')
{
str2[8]++;
}
else if(str1[i]=='j' || str1[i]=='J')
{
str2[9]++;
}
else if(str1[i]=='k' || str1[i]=='K')
{
str2[10]++;
}
else if(str1[i]=='l' || str1[i]=='L')
{
str2[11]++;
}
else if(str1[i]=='m' || str1[i]=='M')
{
str2[12]++;
}
else if(str1[i]=='n' || str1[i]=='N')
{
str2[13]++;
}
else if(str1[i]=='o' || str1[i]=='O')
{
str2[14]++;
}
else if(str1[i]=='p' || str1[i]=='P')
{
str2[15]++;
}
else if(str1[i]=='q' || str1[i]=='Q')
{
str2[16]++;
}
else if(str1[i]=='r' || str1[i]=='R')
{
str2[17]++;
}
else if(str1[i]=='s' || str1[i]=='S')
{
str2[18]++;
}
else if(str1[i]=='t' || str1[i]=='T')
{
str2[19]++;
}
else if(str1[i]=='u' || str1[i]=='U')
{
str2[20]++;
}
else if(str1[i]=='v' || str1[i]=='V')
{
str2[21]++;
}
else if(str1[i]=='w' || str1[i]=='W')
{
str2[22]++;
}
else if(str1[i]=='x' || str1[i]=='X')
{
str2[23]++;
}
else if(str1[i]=='y' || str1[i]=='Y')
{
str2[24]++;
}
else if(str1[i]=='z' || str1[i]=='Z')
{
str2[25]++;
}
/*else
{
break;
}*/
}
for(k=0;k<26;k++)
{
cout << str2[k] << endl ;
}
cout << endl;
open.close();
return 0;
}char histogram::Table()
{
cout <<"\t"<< "Alphabets" <<"\t"<< "Frequency" <<"\t"<< "Probability" <<endl;
}
int main()
{
histogram a;
cout << a.OpenText() << endl;
//cout << a.Table() << endl;
system("pause");
return 0;
}

Wow, just looking at that code makes me want to take a garden fork and gouge my eyes out with it.
Please clean up your code a bit an repost.
Ex.
The long string of "if" statements could be done just as well, but with less confusion, doing this:
switch(str1[i])
{
case 'a':
case 'A':
str2[0]++;
break;
case 'b':
case 'B':
str2[1]++;
break;
...
default:
//error? Not sure sure what you're trying to do
break;
}
However, even that is tedious, and using the basic tidbit of knowledge that characters are stored using ASCII, the entire thing could be implemented as follows:
char letter = tolower(str1[i]);
str2[str1[i]-'a']++;
Note that tolower() has no error checking, so you'll need to make sure that str1[i] is a letter yourself.BlueRaja.admin@gmail.com

![]() |
Eiffel (help) basic
|
deleting folders - use ba...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |