>but you know, I prefere to try and write my own
That's why I showed you an example that couldn't possibly be accepted as homework. It's just a way for me to show off. ;)
>we're being teached this way of programming and you say that it's the old way, can you explain this a bit more
Before C++ was standardized, everyone basically agreed to use headers such as iostream.h as defined by The Annotated C++ Reference Manual written by Bjarne Stroustrup. When the language was standardized in 1998, these headers were edited heavily and the names were changed to remove the .h extension. You're being taught an old dialect of C++ that is quickly dying out.
>can you explain why because I'm not asking a return from main
If your compiler doesn't handle standard C++ then not returning a value from main results in undefined behavior. Undefined behavior is one of the worst things that your programs can have because it means they can do anything they like including (but not limited to) destructive operations on your computer such as wiping out data. Standard C++ defines main to return 0 (for success) if execution falls off the end, so you can omit a return statement safely.
However, just because you don't return a value explicitly doesn't mean that you can change the definition of main, which the standard states shall be (and when they use the word shall, you'd better do it or suffer undefined behavior):
int main()
or
int main ( int argc, char *argv[] )
or anything equivalent
Telling main to return void is undefined behavior, but in practice it's entirely possible that the program would simply fail to run if the run-time startup code sees a different call/return than it expected. Sadly, several systems support void main as an extension, but keep in mind that it'sonly an extension. Your code is not portable.
>Well, we we're told to use ASCII
You have to do what you have to do. Just remember that by assuming ASCII your code isn't as portable as it could be.
>I don't know how to solve that problem
ASCII letters are in sequential order. You can take advantage of that to create an array of 26 rather than an array of 128:
#include <cctype>
#include <iostream>
using namespace std;
int main()
{
char letters[] = "abcdefghijklmnopqrstuvwxyz";
int freq[sizeof letters - 1] = {0};
char str[80];
cout<<"Enter a string: ";
if ( cin.getline ( str, sizeof str ) ) {
for ( int i = 0; str[i] != '\0'; i++ ) {
if ( isalpha ( str[i] ) )
++freq[tolower ( str[i] ) - 'a'];
}
for ( int i = 0; letters[i] != '\0'; i++ ) {
if ( freq[i] != 0 )
cout<< letters[i] <<": "<< freq[i] <<endl;
}
}
}
Otherwise you would need to account for every character in the character set, then discard what you don't want manually:
#include <cctype>
#include <iostream>
using namespace std;
int main()
{
int freq[128] = {0};
char str[80];
cout<<"Enter a string: ";
if ( cin.getline ( str, sizeof str ) ) {
for ( int i = 0; str[i] != '\0'; i++ ) {
if ( isalpha ( str[i] ) )
++freq[tolower ( str[i] )];
}
for ( int i = 0; i < 128; i++ ) {
if ( isalpha ( static_cast<unsigned char> ( i ) ) && freq[i] != 0 )
cout<< static_cast<unsigned char> ( i ) <<": "<< freq[i] <<endl;
}
}
}
Neither is better than the other simply because the former is more efficient for your purposes, but the latter is efficient if you need a more general frequency table for the character set.
>And newer compilers still accept the old way as an alternative to remain compatible with old code.
This is changing though. For example, Visual Studio .NET fails to compile prestandard C++.
>It's not strictly required I think (the compiler won't complain after all)
It is strictly required, and a compiler that doesn't complain is probably set to allow non-standard extensions and void main is one of them. But when set to disable extensions, all decent compilers should flag a warning.
>with 0 meaning (on most operating systems) there was no error
0 is success for all implementations, on all systems, or it's not C++.
>Narue, am I correct when I say that your code is written in OO?
Yes and no. I make use of standard objects, but the term generic programming (through templates) would be more appropriate than object oriented programming in this case.
>- Art of Computer Programming Volumes 1-3 Box set
Good choice. :) These are among my favorites. They're essential for any professional or hardcore hobbyist.