Absolute C++ programming cahllenge problem
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Absolute C++ programming cahllenge problem

  1. #1
    Join Date
    Feb 2007
    Posts
    9

    Absolute C++ programming cahllenge problem

    There is a programming project that I just don't know how to implement. Here is what it is asking:

    My mother always took a little red counter to the grocery store. The counter was used to keep tally of the amount of money she would have spent so far on that visit to the store if she bought everything in the basket. The counter had a four digit display, increment buttons for each digit, and a reset button. An overflow indicator came up red if more money was entered than the $99.99 it would register.

    Write and implement the member functions of a class Counter that simulates and slightly generalizes the behavior of this grocery store counter. The constructor should create a Counter object that can count up to the constructor's argument. That is Counter(9999) should provide a counter that can count up to 9999. A newly constructed counter displays a reading of 0. The member function void reset(); sets the counter's number to 0. The member function void incr1(); increments the units digits by 1, void incr10(); increments the tens digit by 1, and void100(); and void1000(); increments the next two digits respectively. Accounting for any carrying when you increment should require no further action than adding an appropriate number to the private data member. A member function bool overFlow(); detects overflow.

    This is what I have so far:

    #include <iostream>
    using namespace std;

    class Counter {
    public:
    Counter(int cents, int dimes, int dollars, int tensDollars);

    void reset();
    void incr1(),
    incr10(),
    incr100(),
    incr1000();
    bool overflow();

    };

    int main() {
    Counter(9, 9, 9, 9);
    cout << "Please enter the amount of cents, dimes, dollars and tens of dollars \n";
    cout << "by using keys a (for cents), s (for dimes), d (for dollars), f (for tens dollars) \n";
    cout << "and o (for overflow) followed by numbers 1-9 to specify the amount of cents, dimes etc. \n";
    cout << "Please enter amount of cents:\n";
    cin >> money.incr1;
    cout << "Please enter amount of dimes:\n";
    cin >> money.incr10;
    cout << "Please enter amount of dollars:\n";
    cin >> money.incr100;
    cout << "Please enter amount of tens pf dollars:\n";
    cin >> money.incr1000;

    return 0;
    }



    How would I implement this idea into C++ code? just some hints, I can't figure this one out, any hint would be greatly appreciated. If anyone has the book and can help me, I would be very thankful

    Thanks in advance.

  2. #2
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Absolute C++ programming cahllenge problem

    Code:
                    unsigned short int cents = 0, dimes = 0, dollars = 0, tensOfDollars = 0;
                     
                    //You have to pass a variable to a function, you can't use cin in your example
    	cout << "Please enter amount of cents:\n";
    	cin >> cents;
                    money.incr1(cents)
    	cout << "Please enter amount of dimes:\n";
    	cin >> dimes;
                    money.incr1(dimes)
    	cout << "Please enter amount of dollars:\n";
    	cin >> dollars;
                    money.incr1(dollars)
    	cout << "Please enter amount of tens of dollars:\n";
                    cin >> tensOfDollars;
                    money.incr1(tensOfDollars)
    So, your class definition would look something like this:

    Code:
    class Counter {
    public:
    Counter(int cents, int dimes, int dollars, int tensOfDollars);
    
    void reset();
    void incr1(int& cents)
    {
    Cents = cents;
    }
    void incr10(int& dimes)
    {
    Dimes = dimes;
    }
    void incr100(int& dollars)
    {
    Dollars = dollars;
    }
    void incr1000(int& tensOfDollars)
    {
    TensOfDollars = tensOfDollars;
    }
    bool overflow();
    
    private:
         int Cents;
         int Dimes;
         int Dollars;
         int TensOfDollars;
    
    };
    Good luck!
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  3. #3
    Join Date
    Oct 2005
    Location
    Bangalore
    Posts
    1,051

    Re: Absolute C++ programming cahllenge problem

    - Sreehari
    "Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us."
    " Everybody is sent to Earth on a purpose. I am so Lagging behind that i won't die." Calvin

  4. #4
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,471

    Re: Absolute C++ programming cahllenge problem

    The description calls for a constructor:
    Code:
    Counter(int maxCents);
    not one taking amounts of different units. The rest of the description implies that you internally work entirely in cents. The rest requires you to work out which of the four functions to call (the appropriate number of times) to add the specified amount to the counter.

    At this point I have to say that, if anyone presented me with a design for this problem as per the description given above, then I would bounce it right back at them and say "try again". The interface required for that class is utterly dreadful, and betrays a common failing: that of letting the real world object take over your thinking. A real-world counter has a max amount and separate buttons because of mechanical limitations. These limitations do not apply to a C++ class, so why artificially introduce them?

    It's all very well making up examples to demonstrate particular facets of the language, but why do so many of them have to be so badly designed? At least put a huge warning against the example: "WARNING: this example is contrived and may contain bad design practices. It should not be treated as representative of real-world code".
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  5. #5
    Join Date
    Feb 2007
    Posts
    9

    Re: Absolute C++ programming cahllenge problem

    Thanks for the replies, I have been busy with work and would have replied sooner.

    I'll try it out and get back to you later on tonight or tomorrow afternoon. Once again a big thanks for the examples.

  6. #6
    Join Date
    Feb 2007
    Posts
    9

    Re: Absolute C++ programming cahllenge problem

    Quote Originally Posted by Mybowlcut
    Code:
                    unsigned short int cents = 0, dimes = 0, dollars = 0, tensOfDollars = 0;
                     
                    //You have to pass a variable to a function, you can't use cin in your example
    	cout << "Please enter amount of cents:\n";
    	cin >> cents;
                    money.incr1(cents)
    	cout << "Please enter amount of dimes:\n";
    	cin >> dimes;
                    money.incr1(dimes)
    	cout << "Please enter amount of dollars:\n";
    	cin >> dollars;
                    money.incr1(dollars)
    	cout << "Please enter amount of tens of dollars:\n";
                    cin >> tensOfDollars;
                    money.incr1(tensOfDollars)
    So, your class definition would look something like this:

    Code:
    class Counter {
    public:
    Counter(int cents, int dimes, int dollars, int tensOfDollars);
    
    void reset();
    void incr1(int& cents)
    {
    Cents = cents;
    }
    void incr10(int& dimes)
    {
    Dimes = dimes;
    }
    void incr100(int& dollars)
    {
    Dollars = dollars;
    }
    void incr1000(int& tensOfDollars)
    {
    TensOfDollars = tensOfDollars;
    }
    bool overflow();
    
    private:
         int Cents;
         int Dimes;
         int Dollars;
         int TensOfDollars;
    
    };
    Good luck!
    Thanks for this one, but what are the money.incr1(cents) and all those in the main body of code for?

  7. #7
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: Absolute C++ programming cahllenge problem

    Off-topic:
    Quote Originally Posted by Graham
    The description calls for a constructor:
    Code:
    Counter(int maxCents);
    not one taking amounts of different units. The rest of the description implies that you internally work entirely in cents. The rest requires you to work out which of the four functions to call (the appropriate number of times) to add the specified amount to the counter.
    So, I think that the OP has a better design than the one expected.

    Because:
    The rest of the description implies that you *internally* work entirely in cents
    When, from the public interface, you can feel internals so much, there's probably something wrong...

    At this point I have to say that, if anyone presented me with a design for this problem as per the description given above, then I would bounce it right back at them and say "try again". The interface required for that class is utterly dreadful, and betrays a common failing: that of letting the real world object take over your thinking. A real-world counter has a max amount and separate buttons because of mechanical limitations. These limitations do not apply to a C++ class, so why artificially introduce them?
    Sad, but true.
    Actually, it's pretty hard. No, It's very hard, to come up with simple object based examples in order to explain object based programming, to students who have never programmed large (i.e. with three or four files whose total size is more than 100KiB), *useful*, projects.

    I'll perhaps say an ultimate heresy, but, showing concepts whose usefulness can't be feeled, is probably not a great idea.

    Anyway, it's possible to write meaningful object based designs, for relatively simple projects.

    There is one notion that, IMHO, should really not be introduced too early: Inheritance.

    I think inheritance shouldn't be introduced before students start making projects where they feel the need of something like inheritance.
    Introducing inheritance day 1, promotes abuses of inheritance, and, since students don't understand at all WHY and WHERE inheritance is useful, they will use it like a super-data-goto...

    Analogy: In biology, do you know how we can understand the usefulness of something?
    Surely, not with a passive observation of something that seems to work.
    There is mainly two means:
    1) Looking at something broken.
    2) Breaking something, and looking at it.

    Otherwise, we can't understand how it works, and even if we could understand how it works, we couldn't understand to which extent it's useful, which are the really useful facets, and why.

    Without trying to write non-OO code (at least in the mind), I think it's hard to understand why and how OO is useful.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  8. #8
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: Absolute C++ programming cahllenge problem

    Quote Originally Posted by Mybowlcut
    void incr10(int& dimes)
    {
    Dimes = dimes;
    }
    I don't understand your design.
    1) incr10 isn't supposed to take any parameter!
    2) Why do you pass by non-const reference this int, since you don't use it as a lvalue?
    3) Dimes = dimes; This is not at all what this function is supposed to do.

    Without changing your private data structure, I think that:
    Code:
    void incr10()
    {
    Dimes++;
    }
    should be right.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center