Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 10-14-2004, 02:33 PM   #31 (permalink)
Androto
Mac Os X User(I hate win)
 
Join Date: Oct 2004
Posts: 138
Androto is on a distinguished road
DELETED. BUT READ PREVIOUS POST.
Androto is offline   Reply With Quote
Old 10-14-2004, 02:44 PM   #32 (permalink)
gamehead200
Code Monkey
 
gamehead200's Avatar
 
Join Date: Oct 2004
Posts: 57
gamehead200 is an unknown quantity at this point
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;
gamehead200 is offline   Reply With Quote
Old 10-14-2004, 02:59 PM   #33 (permalink)
Androto
Mac Os X User(I hate win)
 
Join Date: Oct 2004
Posts: 138
Androto is on a distinguished road
how would i use that in the code?
Androto is offline   Reply With Quote
Old 10-14-2004, 03:07 PM   #34 (permalink)
gamehead200
Code Monkey
 
gamehead200's Avatar
 
Join Date: Oct 2004
Posts: 57
gamehead200 is an unknown quantity at this point
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!
gamehead200 is offline   Reply With Quote
Old 10-14-2004, 03:13 PM   #35 (permalink)
Androto
Mac Os X User(I hate win)
 
Join Date: Oct 2004
Posts: 138
Androto is on a distinguished road
what is switch?
Androto is offline   Reply With Quote
Old 10-14-2004, 03:30 PM   #36 (permalink)
Androto
Mac Os X User(I hate win)
 
Join Date: Oct 2004
Posts: 138
Androto is on a distinguished road
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.
Androto is offline   Reply With Quote
Old 10-14-2004, 04:22 PM   #37 (permalink)
Androto
Mac Os X User(I hate win)
 
Join Date: Oct 2004
Posts: 138
Androto is on a distinguished road
Deleted (i didn't want to triple post)
Androto is offline   Reply With Quote
Old 10-14-2004, 05:09 PM   #38 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
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.
__________________
Valmont is offline   Reply With Quote
Old 10-14-2004, 05:32 PM   #39 (permalink)
Androto
Mac Os X User(I hate win)
 
Join Date: Oct 2004
Posts: 138
Androto is on a distinguished road
lol. k, i'll be back
Androto is offline   Reply With Quote
Old 10-14-2004, 05:57 PM   #40 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
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.
Valmont is offline   Reply With Quote
Old 10-14-2004, 06:28 PM   #41 (permalink)
Androto
Mac Os X User(I hate win)
 
Join Date: Oct 2004
Posts: 138
Androto is on a distinguished road
THX ALOT!!!!
Androto is offline   Reply With Quote
Old 10-14-2004, 06:32 PM   #42 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
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?
__________________
Valmont is offline   Reply With Quote
Old 10-14-2004, 06:37 PM   #43 (permalink)
Androto
Mac Os X User(I hate win)
 
Join Date: Oct 2004
Posts: 138
Androto is on a distinguished road
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?
Androto is offline   Reply With Quote
Old 10-14-2004, 06:38 PM   #44 (permalink)
Androto
Mac Os X User(I hate win)
 
Join Date: Oct 2004
Posts: 138
Androto is on a distinguished road
one more thing, the ctrl-d or ctrl-z thing doesn't work, but when i hit enter it works fine
Androto is offline   Reply With Quote
Old 10-14-2004, 06:41 PM   #45 (permalink)
Androto
Mac Os X User(I hate win)
 
Join Date: Oct 2004
Posts: 138
Androto is on a distinguished road
while( !(getline(cin, sSentence)) || sSentence.size()==0 )

i don't understand this part
Androto is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Deadlock Detection Program Help... coolsc81 Standard C, C++ 2 10-26-2004 06:14 AM
Help on starting new program B00tleg Standard C, C++ 21 10-17-2004 12:58 PM
Need help on program B00tleg Standard C, C++ 1 10-12-2004 12:02 AM
Help on interest program B00tleg Standard C, C++ 2 10-07-2004 08:50 PM


All times are GMT -8. The time now is 11:42 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting