View Single Post
Old 10-14-2004, 08: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