Thread: Fill in the blanks

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Code:
    int main() try
    {
    Was that part of the challenge (the missing opening brace for main)? I thought that was what dwks was trying to point out, and my interpretation of the challenge was that it had little to do with tricks and more to do with good code design.

  2. #17
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Was that part of the challenge (the missing opening brace for main)?
    Hmm, I'm starting to think that you guys aren't familiar with the function try block feature. It was originally designed to work in conjunction with initialization lists in constructors:
    Code:
    Constructor()
    try
        : a(), b() // Might throw
    {
    }
    catch (...)
    {
        // Handle an exception from a or b
    }
    But you can use it with main just as easily to handle exceptions anywhere in the body:
    Code:
    int main() try
    {
        // Stuff
    }
    catch (...)
    {
        // Handle any exception thrown
    }
    It wasn't a trick, I swear!
    My best code is written with the delete key.

  3. #18
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Nope, I've never seen that before. But I knew it wasn't a typo, because Prelude being . . . well . . . Prelude.

    I suppose we could just have run it through the online compilers that were mentioned.

    Code:
    Dinkum Exam? page results:
    
    Your code has been compiled with the Microsoft Visual Studio 2005 C++ compiler using
    the Dinkum C++ library from the Dinkum Compleat Libraries for VC++ package.
    
    This is the compiler output using the code above in a file named
    sourceFile.cpp:
    
    --------------------
    
    sourceFile.cpp
    sourceFile.cpp(6) : error C2653: 'jsw' : is not a class or namespace name
    sourceFile.cpp(6) : error C2065: 'Password' : undeclared identifier
    sourceFile.cpp(6) : error C2146: syntax error : missing ';' before identifier 'password'
    sourceFile.cpp(6) : error C3861: 'password': identifier not found
    sourceFile.cpp(8) : error C2228: left of '.Get' must have class/struct/union
            type is ''unknown-type''
    sourceFile.cpp(10) : error C2653: 'std' : is not a class or namespace name
    sourceFile.cpp(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    sourceFile.cpp(10) : error C2146: syntax error : missing ';' before identifier 'prompt'
    sourceFile.cpp(10) : error C2065: 'prompt' : undeclared identifier
    sourceFile.cpp(10) : error C2059: syntax error : ']'
    sourceFile.cpp(10) : error C2143: syntax error : missing ';' before '{'
    sourceFile.cpp(10) : error C2143: syntax error : missing ';' before '}'
    
    ...
    
    --------------------
    
    Code failed to compile! Compiler generated output, but no executable was generated.
    
    All rights reserved 2006 by Dinkumware, Ltd.
    Copyright (c) 1996-2006 by Dinkumware, Ltd.
    Dinkumware, Dinkum, Jcore, and Proofer are registered trademarks of Dinkumware, Ltd.
    The first error is on line 6.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #19
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I have gotten it to compile, using only the Dinkumware online compiler!
    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <vector>
    #include <exception>
    
    namespace jsw {
        class Password {
        private:
            std::string password;
            const int tries;
        public:
            Password(std::string pw, int t) : password(pw), tries(t) {}
            bool Get(std::string prompt);
        };
    
        bool Password::Get(std::string prompt) {
            std::string pw;
    
            for(int x = 0; x < tries && pw != password; x ++) {
                std::cout << prompt;
                std::getline(std::cin, pw);
            }
    
            return pw == password;
        }
    
        class Selection {
        private:
            struct item_t {
                int value;
                std::string prompt;
            };
            std::vector<item_t> prompts;
        public:
            void Add(int rv, std::string prompt);
            int Get(std::string prompt);
        };
    
        void Selection::Add(int rv, std::string prompt) {
            item_t item;
            item.value = rv;
            item.prompt = prompt;
            prompts.push_back(item);
        }
    
        int Selection::Get(std::string prompt) {
            int v;
            for(;;) {
                for(int x = 0; x < prompts.size(); x ++) {
                    std::cout << prompts[x].prompt << std::endl;
                }
    
                std::cout << prompt;
                if((std::cin >> v) == 0) throw std::exception();
    
                for(int x = 0; x < prompts.size(); x ++) {
                    if(v == prompts[x].value) return x;
                }
            }
        }
    
        class Input {
        public:
            template <typename T>
            static T Get(istream &i) { T x; i >> x; return x; }
        };
    
        class MonetaryConversion {
        private: 
            static const double euroRate = 1.27155;
        public:
            static double EuroToDollar(double x) { return x * euroRate; }
            static double DollarToEuro(double x) { return x / euroRate; }
        };
    }
    
    // start of Prelude's code
    
    int main() try
    {
        const int tries = 3;
    
        // TODO: Not hardcode requested password
        jsw::Password password ( "skip", tries );
    
        if ( password.Get ( "Please enter your password: " ) )
        {
            const std::string prompt[] = { "Euros: ", "Dollars: " };
            const std::string result[] = { "In Dollars: ", "In Euros: " };
            enum { euro_to_dollar = 1, dollar_to_euro };
    
            jsw::Selection menu; // Menu controller
            int selector;        // Menu selector
            double value;        // Value to convert
    
            // Set up and process menu
            menu.Add ( euro_to_dollar, "1) Euros to Dollars" );
            menu.Add ( dollar_to_euro, "2) Dollars to Euros" );
            selector = menu.Get ( "Selection: " );
    
            // Set up and process conversion
            std::cout<< prompt[selector - 1];
            value = jsw::Input::Get<double> ( std::cin );
            std::cout<< result[selector - 1];
    
            std::cout.setf ( std::ios::fixed, std::ios::floatfield );
            std::cout.precision ( 2 );
    
            if ( selector == euro_to_dollar )
                std::cout<< jsw::MonetaryConversion::EuroToDollar ( value ) <<'\n';
            else
                std::cout<< jsw::MonetaryConversion::DollarToEuro ( value ) <<'\n';
        }
        else
            std::cerr<<"Too many incorrect passwords. Closing...\n";
    }
    catch ( const std::exception& ex ) {
        std::cerr<< ex.what() <<'\n';
    }
    Does it count, Prelude? I throw an exception if the user enters an invalid number. It's probably not the best place to throw an exception; but I wanted to keep it simple (the text-entering box for Dinkumware doesn't have syntax highlighting; you don't know how much you depend on it until it's missing).

    Why did you choose the name "jsw", by the way? Just curious.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #20
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Not bad, you have the basic idea, but it doesn't work. Though I'm sure that's due to not being able to test it, so I won't be mean to you.

    >Does it count, Prelude?
    Not if it doesn't compile. You should try the Comeau compiler, it's good about forcing strict C++.

    >It's probably not the best place to throw an exception
    Not really, especially since you can recover from that in the function with little trouble.

    >Why did you choose the name "jsw", by the way? Just curious.
    Julienne Sandra Walker. I use my intials for my personal namespace.
    My best code is written with the delete key.

  6. #21
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Okay, I'll submit a tested version when I have access to a true compiler.

    >It's probably not the best place to throw an exception
    Not really, especially since you can recover from that in the function with little trouble.
    I know but I thought I should throw an exception somewhere due to the fancy catch block.

    >Why did you choose the name "jsw", by the way? Just curious.
    Julienne Sandra Walker. I use my intials for my personal namespace.
    I use DWK for the same reason.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #22
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Not bad, you have the basic idea
    Sorry for quoting myself, but I just wanted to note that I was talking about in relation to my solution. It doesn't mean in any way that I'm barring other solutions, especially super creative ones. Part of the fun is seeing how everyone does it differently.

    >I know but I thought I should throw an exception somewhere due to the fancy catch block.
    Well, technically you don't need to throw anything if you don't want to. The catch would simply end up being extraneous characters in the source file.
    My best code is written with the delete key.

  8. #23
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    >I know but I thought I should throw an exception somewhere due to the fancy catch block.
    Well, technically you don't need to throw anything if you don't want to. The catch would simply end up being extraneous characters in the source file.
    You do in Java. Yes, I know. It seemed such a waste to ignore that try-catch, though.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fill the blanks in the program...
    By sreeramu in forum C Programming
    Replies: 19
    Last Post: 10-18-2007, 05:26 AM
  2. Question On how to Fill icmp_data
    By Antigloss in forum Linux Programming
    Replies: 3
    Last Post: 04-25-2007, 09:42 PM
  3. Question regarding the fill function.
    By ChristianTool in forum C++ Programming
    Replies: 2
    Last Post: 03-13-2005, 09:08 PM
  4. Replies: 3
    Last Post: 12-22-2004, 07:29 PM
  5. please help remove blanks from string
    By cjtotheg in forum C Programming
    Replies: 2
    Last Post: 10-24-2001, 12:21 PM