Implement member function Homework

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jas1936
    New Member
    • Mar 2010
    • 2

    Implement member function Homework

    I am working on a homework assignment for my C++ class. Here is the homework description:

    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
    void incr100( ); and
    void incr1000( ); increment 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. (Overflow is the result of incrementing the
    counter’s private data member beyond the maximum entered at counter
    construction.)

    Use this class to provide a simulation of my mother’s little red clicker. Even
    though the display is an integer, in the simulation, the rightmost (lower
    order) two digits are always thought of as cents and tens of cents, the next
    digit is dollars, and the fourth digit is tens of dollars.
    Provide keys for cents, dimes, dollars, and tens of dollars. Unfortunately, no
    choice of keys seems particularly mnemonic. One choice is to use the keys
    asdfo: a for cents, followed by a digit 1 to 9; s for dimes, followed by a
    digit 1 to 9; d for dollars, followed by a digit 1 to 9; and f for tens of
    dollars, again followed by a digit 1 to 9. Each entry (one of asdf
    followed by 1 to 9) is followed by pressing the Return key. Any overflow is
    reported after each operation. Overflow can be requested by pressing the o key.


    This is the code that I have so far

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    class Counter{
          public:
                 void incr1(); //increments the units digits by 1
                 void incr10();//increments the tens digit by 1
                 void incr100();
                 void incr1000();//increment the next two digits, respectively
                 void reset();
                 bool overflow();
                 void displayCounter();
          private:
                  int jCents;
                  int jDimes;
                  int jDollars;
                  int jTens;
    };
    
    int main()
    {
        Counter clicker;
        char letter;
        char digit;
        bool booleanFlag;
        clicker.reset();
        
        cout << "Enter A Character Followed By A Digit From 1-9:" << endl;
        cout << "Enter 'a' For Cents\n 's' For Dimes\n 'd' For Dollars\n 'f' For Tens\n 'o' To Inquire About Overflow\n 'r' To Reset\n Q/q To End\n";
        cin >> letter >> digit;
    
    // Counter::incr1(int)  
        while(letter != 'Q' || letter != 'q'){
                     
                     switch(letter)
                     {
                                   case 'a':
                                        clicker.incr1();
                                        break;
                                   case 's':
                                        clicker.incr10();
                                        break;
                                   case 'd':
                                        clicker.incr100();
                                        break;
                                   case 'f':
                                        clicker.incr1000();
                                        break;
                                   case 'o':
                                        clicker.overflow();
                                        break;
                                   case 'r':
                                        clicker.reset();
                                        break;
                     }
                     
        cout << "Enter 'a' For Cents\n 's' For Dimes\n 'd' For Dollars\n 'f' For Tens\n 'o' To Inquire About Overflow\n 'r' To Reset\n Q/q To End\n";
        cin >> letter;
    }
    }  // end of main
    
    
        void Counter::incr10(){
             jCents += digit;
             if(jCents > 9){
                    jCents = jCents - 9;
                    jDimes += 1;
                    }
             }
             
        
        void Counter::incr1(){
             jCents += digit;
             if(jCents > 9){
                    jCents = 0;
                    jDimes += 1;
                    }
             }
             
        
        void Counter::incr10(){
             jDimess += digit;
             if(jDimess > 9){
                    jDimes = jDimes - 9;
                    jDollars += 1;
                    }
             }
             
        
        void Counter::incr100(){
             jDollars += digit;
             if(jDollarss > 9){
                    jDollars = jDollars - 9;
                    jTens += 1;
                    }
             }
             
        void Counter::incr1000(){
             if (jTens > 9){
                       overflow();
                       }
                       jTens += digit;
             }
             
        bool Counter::overflow(){
             cout << "OVERFLOW HAS OCCURRED. RESULTS ARE NOT RELIABLE. Press Q to quit.";
             reset();
             }
        
        void Counter::reset(){
             jCents = 0;
             jDimes = 0;
             jDollars = 0;
             jTens = 0;
             
             }
                                    
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    My code is not working for some reason, I have no clue what I did wrong. Could someone please help me figure out what it is I did wrong?

    Thanks in advance
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    "My code is not working" is a very poor description of your problem.

    What was your observed behaviour of the program?
    What was the desired behaviour of the program?
    What input did you give the program to get the observed behaviour?
    What output did the program give?

    1 thing I would say though is that you have made you Counter class overly complex by having separate member variables for cents, dimes dollars and tens when you could use a single variable to just contain the value in cents and you have no member variable to contain the maximum value that can be counted and no constructor to initialise it.

    Also line 121 - 123 seem to be the end of main yet your listing indicates main ends at line 62. You can define functions inside other functions so lines 121 - 123 look like you have accidetally left them in or not moved them to line 62.

    Comment

    • Jas1936
      New Member
      • Mar 2010
      • 2

      #3
      Ok, I see what you mean. I will move lines 121-123. I know that my class is overly complecated, but that is how my professor told me to do it.

      The program is supposed to increment the money type that is selected by the user (ex: dimes). It should increment the amount of times that the user selects(ex: dimes, 9, should equal 90 cents)

      When I ran my program it listed the options, but it did not increment the counter, and it did not reset.

      I entered in "S 2" for dimes, incremented twice, which shuold have been 20 cents.

      The program just started over from the beginning without displaying the "0.20" that was expected.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        I think you must have done more than just more those lines. The way that the posted code uses digit would produce compiler errors.

        digit is local to main so you class methods can not use it. Main should be calling the relevant incr* function digit times but main actually ignores the value of digit having read it in.

        The posted main function never outputs the value so that is why you are not seeing it.

        Finally in the posted code each incr* function trys to handle overflow by itself, when jDimess overflows incr10 increments jDollars itself. However that does not handle overflow in jDollars, it would be better to call incr100 which does handle overflow in jDollars. And the same for the other class methods.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          I suggest you keep the money in pennies. That is, 1234 is the same as $12.34 and I'm sure you can convert 1234 to $12.34:

          dollars = money/100
          cents = money % 100

          So display a $ then display dollars then display a period then display cents. And you are done.

          If you need nickels:

          nickels = money/5
          cents = money % 5

          etc...

          Comment

          Working...