At 05:19 2007-01-21, David wrote:
>Rick a écrit :
> > At 1/20/2007 08:50 PM, you wrote:
> >> /// \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>
>replace by
>#include <stdexcept>
thank you, on my system, one of the other
#includes picked it up...not sure which one
> >> #include <numeric>
> >> #include <algorithm>
> >> #include <iomanip>
> >>
>you can remove it
I'd planned to format the output some like maybe
either a << right << or << setw(something) << then forgot
> >> 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){}
>
>may be explicit ?
yes, it could be explicit. Since it's ONLY use
is for a throw, I don't think anyone is going to get an accidental conversion
> >> };
> >>
> >> /// 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";
>may be a return 1; here
could be... I didn't really consider it an error
as such. I often run my command line utilities
with NO arguments to get reminded what the args
and options are (no options here, of course)
> >> }
> >> /// 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;
>
>you can remove return 0; here
old habit... I usually write the int main(int
argc, char* argv[]) { return 0; } early on, then go insert the actual code
thanks for the comments (and the fix (stdexcept))
> >> }
>
>
>
>
>To unsubscribe, send a blank message to
><mailto:[EMAIL PROTECTED]>.
>Yahoo! Groups Links
>
>
>
Victor A. Wagner Jr. http://rudbek.com
The five most dangerous words in the English language:
"There oughta be a law"