|
 |
|
 |
10-14-2004, 02:33 PM
|
#31 (permalink)
|
|
Mac Os X User(I hate win)
Join Date: Oct 2004
Posts: 138
|
DELETED. BUT READ PREVIOUS POST.
|
|
|
10-14-2004, 02:44 PM
|
#32 (permalink)
|
|
Code Monkey
Join Date: Oct 2004
Posts: 57
|
Because I'm nice and its due tomorrow, here's part of my code:
Code:
while( i < sent.size() )
{
switch( sent[i] )
{
case 'a':
acount++;
break;
}
i++;
}
And to only show the letters that actually appear more than 0 times:
Code:
if( acount )
cout << "a = " << acount << endl;
|
|
|
10-14-2004, 02:59 PM
|
#33 (permalink)
|
|
Mac Os X User(I hate win)
Join Date: Oct 2004
Posts: 138
|
how would i use that in the code?
|
|
|
10-14-2004, 03:07 PM
|
#34 (permalink)
|
|
Code Monkey
Join Date: Oct 2004
Posts: 57
|
See the acount++? You need to create an integer with the name acount (or whatever your letter is) and set it to 0... Once it finds the letter a in your sentence, it will add 1 to acount... sent is the variable I used for the sentence... You need to create a whole bunch of cases for each letter as well... For the second piece of code, you need to repeat that for each letter as well! 
|
|
|
10-14-2004, 03:13 PM
|
#35 (permalink)
|
|
Mac Os X User(I hate win)
Join Date: Oct 2004
Posts: 138
|
what is switch?
|
|
|
10-14-2004, 03:30 PM
|
#36 (permalink)
|
|
Mac Os X User(I hate win)
Join Date: Oct 2004
Posts: 138
|
WHAT IS WRONG WITH THIS? I GOT LIKE OVER 107 MISTAKES. I'M SURE THERES JUST ONE LITTLE THING I DID WRONG WHICH MADE THE REST OF IT WRONG.
Code:
#include <iostream>
#include <ctsdlib>
#include <strings>
int main()
{
int i(0);
string sent;
string acount;
string bcount;
string ccount;
string dcount;
string ecount;
string fcount;
string gcount;
string hcount;
string icount;
string jcount;
string kcount;
string lcount;
string mcount;
string ncount;
string ocount;
string pcount;
string qcount;
string rcount;
string scount;
string tcount;
string ucount;
string vcount;
string wcount;
string xcount;
string ycount;
string zcount;
letter_counter;
while (i < sent.size)
{
switch( sent[i] )
{
case 'a';
acount++;
break;
}
{
case 'b';
bcount++;
break;
}
{
case 'c';
ccount++;
break;
}
{
case 'd';
dcount++;
break;
}
{
case 'e';
ecount++;
break;
}
{
case 'f';
fcount++;
break;
}
{
case 'g';
gcount++;
break;
}
{
case 'h';
hcount++;
break;
}
{
case 'i';
icount++;
break;
}
{
case 'j';
jcount++;
break;
}
{
case 'k';
kcount++;
break;
}
{
case 'l';
lcount++;
break;
}
{
case 'm';
mcount++;
break;
}
{
case 'n';
ncount++;
break;
}
{
case 'o';
ocount++;
break;
}
{
case 'p';
pcount++;
break;
}
{
case 'q';
qcount++;
break;
}
{
case 'r';
scount++;
break;
}
{
case 's';
scount++;
break;
}
{
case 't';
tcount++;
break;
}
{
case 'u';
ucount++;
break;
}
{
case 'v';
vcount++;
break;
}
{
case 'w';
wcount++;
break;
}
{
case 'x';
xcount++;
break;
}
{
case 'y';
zcount++;
break;
}
{
case 'z';
zcount++;
break;
}
i++;
}
if (acount > 0)
cout << "a = " << acount << endl;
if (bcount > 0)
cout << "b = " << bcount << endl;
if (ccount > 0)
cout << "c = " << ccount << endl;
if (dcount > 0)
cout << "d = " << dcount << endl;
if (ecount > 0)
cout << "e = " << ecount << endl;
if (fcount > 0)
cout << "f = " << fcount << endl;
if (gcount > 0)
cout << "g = " << gcount << endl;
if (hcount > 0)
cout << "h = " << hcount << endl;
if (icount > 0)
cout << "i = " << icount << endl;
if (jcount > 0)
cout << "j = " << jcount << endl;
if (kcount > 0)
cout << "k = " << kcount << endl;
if (lcount > 0)
cout << "l = " << lcount << endl;
if (mcount > 0)
cout << "m = " << mcount << endl;
if (ncount > 0)
cout << "n = " << ncount << endl;
if (ocount > 0)
cout << "o = " << ocount << endl;
if (pcount > 0)
cout << "p = " << pcount << endl;
if (qcount > 0)
cout << "q = " << qcount << endl;
if (rcount > 0)
cout << "r = " << rcount << endl;
if (scount > 0)
cout << "s = " << scount << endl;
if (tcount > 0)
cout << "t = " << tcount << endl;
if (ucount > 0)
cout << "u = " << ucount << endl;
if (vcount > 0)
cout << "v = " << vcount << endl;
if (wcount > 0)
cout << "w = " << wcount << endl;
if (xcount > 0)
cout << "x = " << xcount << endl;
if (ycount > 0)
cout << "y = " << ycount << endl;
if (zcount > 0)
cout << "z = " << zcount << endl;
}
Last edited by Androto; 10-14-2004 at 04:22 PM.
|
|
|
10-14-2004, 04:22 PM
|
#37 (permalink)
|
|
Mac Os X User(I hate win)
Join Date: Oct 2004
Posts: 138
|
Deleted (i didn't want to triple post)
|
|
|
10-14-2004, 05:09 PM
|
#38 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Hey relax. I'm right here. Relax. And come back in an hour or so. It's late and I am busy with stuff, but also doing your homework. Just relax.
__________________
|
|
|
10-14-2004, 05:32 PM
|
#39 (permalink)
|
|
Mac Os X User(I hate win)
Join Date: Oct 2004
Posts: 138
|
lol. k, i'll be back
|
|
|
10-14-2004, 05:57 PM
|
#40 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Give this to your teacher. I don't think you'll understand everything although I did my best to keep it simple and straightforward. Just ask what you want to know.
By the way, I needed to split up theAlphabet initialization because something goes wrong with copying to this board. But you can make it just one line, or leave it like this.
Code:
#include <iostream>
#include <string>
using namespace std;
//Optional function: depends on IDE.
void wait_for_enter();
//The world we are talking about.
const unsigned CHARS = 52;
string theAlphabet = "abcdefghijklmnopqrstuvw"
"qyzABCDEFGHIJKLMNOQRSTUVWXYZ";
unsigned theAlphabetCount[CHARS]={0};
string sSentence;
//Core functions.
void menu();
void count_chars();
void print_stats();
int main()
{
menu();
count_chars();
print_stats();
wait_for_enter();
return 0;
}
////////////////////////////////////////////////
void wait_for_enter()
{
cout << "press <enter> to continue...";
// Reset failstate, just in case.
cin.clear();
string line;
getline( cin, line);
}
/////////////////////////////////////////////////
void menu()
{
cout<<"Enter a sentence please (CTRL-Z/D to abort):"<<endl;
while( !(getline(cin, sSentence)) || sSentence.size()==0 )
{
//If CTRL-Z/D then exit: we don't want to enter anything.
if(cin.eof())
break;
cout<<"Something went wrong, or you didn't enter a sentence. Please try again."<<endl;
//Just in case.
cin.clear();
}
}
/////////////////////////////////////////////////
void count_chars()
{
for(unsigned i=0; i<sSentence.size(); ++i)
for(unsigned j=0; j<CHARS; ++j)
if( sSentence.substr(i,1) == theAlphabet.substr(j,1) )
theAlphabetCount[j]++;
}
//////////////////////////////////////////////////
void print_stats()
{
cout<<"\t\tTHE STATISTICS"<<endl<<endl;
cout<<"\t\t--------------"<<endl<<endl;
cout<<"\t\tAnalized sentence: "<<sSentence<<endl<<endl;
cout<<"\t\tChar:\tCount:"<<endl;
for(unsigned i=0; i<CHARS; ++i)
if(theAlphabetCount[i] > 0)
cout<<"\t\t"<<theAlphabet[i]<<"\t"<<theAlphabetCount[i]<<endl;
}
__________________
Last edited by Valmont; 10-14-2004 at 06:28 PM.
|
|
|
10-14-2004, 06:28 PM
|
#41 (permalink)
|
|
Mac Os X User(I hate win)
Join Date: Oct 2004
Posts: 138
|
THX ALOT!!!!
|
|
|
10-14-2004, 06:32 PM
|
#42 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
I've improved it (very slightly), just a heads up. But that is not why I am replying.
You should tell your teacher that you did get considerable help. Not only from me, but gamehead gave you a critical hint as well! Otherwise your rating won't be fair compared to others.
gamehead200
See how I avoided a long switch statement and millions of variables?
__________________
|
|
|
10-14-2004, 06:37 PM
|
#43 (permalink)
|
|
Mac Os X User(I hate win)
Join Date: Oct 2004
Posts: 138
|
ok, i don't think he's online this late at night, but who knows, i'll tell my teacher and gamehead200 tomorrow morning.
i have a couple of questions:
unsigned theAlphabetIndex[52]={0};
can you explain this, didn't you just difine this in the line before?
why are you using void? what does void mean? does it mean it doesn't return a number?
|
|
|
10-14-2004, 06:38 PM
|
#44 (permalink)
|
|
Mac Os X User(I hate win)
Join Date: Oct 2004
Posts: 138
|
one more thing, the ctrl-d or ctrl-z thing doesn't work, but when i hit enter it works fine
|
|
|
10-14-2004, 06:41 PM
|
#45 (permalink)
|
|
Mac Os X User(I hate win)
Join Date: Oct 2004
Posts: 138
|
while( !(getline(cin, sSentence)) || sSentence.size()==0 )
i don't understand this part
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 11:42 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|