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, 06:42 PM   #46 (permalink)
Androto
Mac Os X User(I hate win)
 
Join Date: Oct 2004
Posts: 138
Androto is on a distinguished road
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]++;
}

this is the last thing i don't get
Androto is offline   Reply With Quote
Old 10-14-2004, 07:05 PM   #47 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
First of all I updated the code. Use the new version.

- The CTRL-Z thing:
Terminating immediatly after CTRL-Z is not an option with me since I support only STANDARD C++ as defined by ISO C++. I'll explain:
If it would terminate immediatly I would need operating specific methods. But standard C++ doesn't know about operating systems. So I don't know about them either.
What happens now, is that something needs to be stored in sSentence. But if CTRL-Z is ENTERED, it is assumed that end-of-file is reached. We trap EOF by using cin.eof and leave that function.

- unsigned theAlphabetIndex[52]={0};
1)I didn't define this before. Read carefully how they are named.
Also I renamed this array because it isn't indexing (well that too as it happens) but its main purpose is to keep track of used character (how often they are used).
2) the "={0}" part means that I am assigning this array with zero's.
I do that because all characters are used 0 times when the application starts.
3) Index 0 refers to letter a, while index 51 refers to the letter Z (capital). That's how it works.

- while( !(getline(cin, sSentence)) || sSentence.size()==0 )
This is a neat one huh?
1) getline() fetches the complete stream, including leading spaces, tabs,etc.
2)but while(!(getline(cin, sSentence) ) means:
"As long as sSentence didn't receive anything VALID..."
3)and while( !(getline(cin, sSentence)) || sSentence.size()==0 )
means:
"As long as sSentence didn't receive anything VALID or sSentence didn't receive anything AT ALL..."

So sSentence needs to get something, and it must be something valid too!
However, EOF is valid as well. So the cin.eof checks if we actually did enter CTRL-Z/D. If so, then we leave the functions instead of asking for input again.

Since sSentence is a std::string, this variable can hold almost anything, if the right functions are used. So IF
!(getline(cin, sSentence))
occurs, something must be terribly wrong! But the chances are slim for that to happen. Nevertheless, we code neat.
__________________
Valmont is offline   Reply With Quote
Old 10-14-2004, 07:19 PM   #48 (permalink)
Androto
Mac Os X User(I hate win)
 
Join Date: Oct 2004
Posts: 138
Androto is on a distinguished road
thx for the explanation and the program. i'm going to add in my own comments to try to better understand the program and how it works. but i get the general idea as to how you used functions.
Androto is offline   Reply With Quote
Old 10-14-2004, 07:20 PM   #49 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Quote:
Originally posted by Androto
Code:
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]++;
}
this is the last thing i don't get
Well this is the smart part basically. A loop in a loop.

Suppose you entered: Hi
Then this happens: sSentence.size() = 2
1) "H" is compared with all characters in the theAlphabet array.
theAlphabet array has 52 different characters in it, so "H" is compared with each of them. "H" is compared 52 times, for each character once.
2)"i" is compared with all characters in the theAlphabet array.
theAlphabet array has 52 different characters in it, so "H" is compared with each of them. "H" is compared 52 times, for each character once.

3) IF one if the characters in the sentence mathces a character in theAlphabet array, that index in theAlphabet array is raised with one.

4)sSentence.substr(i,1)
We need only ONE character of the sentence so we use std::subtr();
std::substr() extracts a part of a string in a std::string!
The first parameter is the starting-index of the string (a std::string is an array too, but a luxury one) and the second parameter is how many characters we want to extract.

Examples:
string result;
string mystring = "Hello everybody";
result=mystring.substr(3,1); // result = "l"
result=mystring.substr(3,3); //result = "lo "
result=mystring.substr(0,3); //result = "Hel"
result=mystring.substr(6,4); //result = "ever"
__________________
Valmont is offline   Reply With Quote
Old 10-14-2004, 07:26 PM   #50 (permalink)
Androto
Mac Os X User(I hate win)
 
Join Date: Oct 2004
Posts: 138
Androto is on a distinguished road
i forgot about that part, good thing you didn't.
Androto is offline   Reply With Quote
Old 10-14-2004, 07:32 PM   #51 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
To gamehead200
Quote:
Originally posted by gamehead200
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;
Hey gamehead, it's a good start.
However, you need a long switch statement and lots of variables. Here is a global solution if you like to move on with coding:
1)Try to decouple things.
Basically ask yourself: What if I change one critical thing, how bad do other parts will perform because they depend on it? Can I make it so other parts don't (or almost don't) depend on it?

2)Make sure you know what you want:
Ask yourself: This is how my function look. But is this function really responsible for so many things?
Look at my code and observe void count_chars().
It does strictly the calculating. Nothing else at all.
While the menu-function is strictly responsible for setting up a menu and processing and validating input.
Actually, the latter is too much already!!! But you're not ready for that yet. Just a heads up though.
And finally the print function. It has some "smartness" in it, the code is so small, I made it part of the function. But I'm moving on the edge already.

Let these two things be your guide next time.
__________________
Valmont is offline   Reply With Quote
Old 10-15-2004, 04:02 AM   #52 (permalink)
Androto
Mac Os X User(I hate win)
 
Join Date: Oct 2004
Posts: 138
Androto is on a distinguished road
what does this mean:
Code:
      //Just in case.
      cin.clear();
Androto is offline   Reply With Quote
Old 10-15-2004, 04:30 AM   #53 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,709
redhead is on a distinguished road
make sure there arent any garbadge in the instream, incase you wanted to get something from it.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-15-2004, 05:23 AM   #54 (permalink)
gamehead200
Code Monkey
 
gamehead200's Avatar
 
Join Date: Oct 2004
Posts: 57
gamehead200 is an unknown quantity at this point
The reason why we have to use the case statements is because our teacher wants us too! By the way, Andrew, if you hand it in like that, he'll probably take off a WHOLE bunch of marks for not use the case statements...

Valmont, I highly appreciate your help... I hope to use this in the future!
gamehead200 is offline   Reply With Quote
Old 10-15-2004, 07:21 AM   #55 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
I had a discussion with collegues some time ago. C++ coders would be better off if half of the schools, universities and institutions stop teaching C++.
What in the name of the world do they think they achieve when practically forcing students to code 26 case statements in a switch?
No wonder that in the beginning Java was so easely sold to employers of sloppy C++ programmers. I think I finally start to see how that was possible.
__________________
Valmont 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 02:58 PM.


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