At 10:54 2007-01-20, Rick wrote:
At 1/20/2007 12:36 PM, you wrote:
On 1/20/07, Rick <<mailto:[EMAIL PROTECTED]>[EMAIL PROTECTED]> wrote:
and your program will blow up..... if you're using C++ forget
about all the damned char* and strxxx() crap
Yes, I know. I forgot to allocate memory for str, but I corrected that.
I'm using C, not C++, so this is perfectly valid. Why get in such a huff?
If you are using iostream and namespace std, you are using C++
-- Brett
Point taken. I don't program C++, but I use the Bloodshed C++
compiler. It put all the header stuff in. (I don't know why it put
#include <cstdlib> and I don't know what that does).
So, would one of you kind souls "fix" this to use C++ strings
instead? I'd love to know the correct way.
~Rick
/// \file
/// $Id: main.cpp 23 2007-01-21 01:40:43Z vawjr $
/// $LastChangedDate: 2007-01-20 18:40:43 -0700 (Sat, 20 Jan 2007) $
/// $LastChangedRevision: 23 $
/// $LastChangedBy: vawjr $
/// $HeadURL: http://rudbekassociates.com/MyOldCVS/Rick/main.cpp $
#include <iostream>
#include <string>
#include <exception>
#include <numeric>
#include <algorithm>
#include <iomanip>
using namespace std;
/// something unique to throw if we find something that's not a "letter"
struct not_a_letter: public runtime_error
{
not_a_letter(char const* reason): runtime_error(reason){}
};
/// this is how you write a function to talk with std::accumulate
/// it calls a function with the current "total" and the new element
unsigned int score_letter(unsigned int sum_so_far, char letter)
{
/// we declare this static so that it only gets initialized
(and allocated)
/// once instead of each time we call the function (which
would be for each
/// character
static int letter_value[] =
{1, 2, 3, 4, 5, 6, 7, 8, 9, // a - i
10, 20, 30, 40, 50, 60, 70,
80, 90, // j - r
100,200,300,400,500,600,700,800};
// s - z
/// is the letter in the range of characters for which we
have scoring values
if ('a' <= letter && letter <= 'z')
{
return sum_so_far + letter_value[letter - 'a'];
}
/// oops, it's not in range, let someone know
/// (and tell them a little bit about what's wrong)
if ('A' <= letter && letter <= 'Z')
throw not_a_letter("we don't allow uppercase letters
in this program.");
throw not_a_letter("we don't allow punctuation in this program.");
}
/// this processes one "word", outputs the word followed by it's score
/// if the score processes throws not_a_letter, we output whatever
message is inside
/// the exception, otherwise we output the total score for the word
void score_word(string const& the_word)
{
cout << the_word << ": "; /// output the word
/// we tell the compiler that we want to look at exceptional
conditions
try
{
/// we start at the betinning (.begn()) and go
/// to the end (.end()) if nothing goes wrong we will have
/// a total to output.
cout << accumulate(the_word.begin(), the_word.end(),
0, score_letter) << '\n';
}
catch (not_a_letter& e)
{
/// output whatever error message decided at the
point the error was found
cout << e.what() << '\n';
}
}
/// Now that we've done the preliminaries, the main program is dead simple
int main(int argc, char* argv[])
{
/// make sure we have something to score (argc will be at
least 1 because the
/// name of the program (as invoked) is put into argv[0] so
there is at least
/// one thing in the argv[] array
if (argc < 2)
{
/// no args... show the way to use the program
cout << "Usage: " << *argv << " word[ word]...\n";
}
/// Ok, we've got some args.... do them all
/// note that we start with argc+1 because the 0th name in
argv[] is the program name
/// argv+argc is one past the end of the of the valid
data. This is how "ranges" are
/// used in the Standard Library i.e. half-open ... incusive
at the beginning,
/// non-inclusive at the end
for_each(argv+1, argv+argc, score_word);
return 0;
}
The output from the above program given the args: Now is the time
for all good men to come to the aid of their party.
is:
Now: we don't allow uppercase letters in this program.
is: 109
the: 213
time: 254
for: 156
all: 61
good: 131
men: 95
to: 260
come: 108
to: 260
the: 213
aid: 14
of: 66
their: 312
party.: we don't allow punctuation in this program.
Press any key to continue . . .
as you can see, the program was run out of the IDE
Victor A. Wagner Jr. http://rudbek.com
The five most dangerous words in the English language:
"There oughta be a law"