Grocery Store counter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blackstormdragon
    New Member
    • Feb 2007
    • 32

    Grocery Store counter

    Here were our instructions:
    "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. (This was a long time ago.)

    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(units), and tens of dollars. Use the following keys: c, d, u, t, o where c is for cents followed by a digit 1 to 9, d is for dimes followed by a digit 1 to 9, u is for dollars followed by a digit 1 to 9, t is for tens followed by a digit 1 to 9, and o is for overflow. Each entry (one of cdut 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."

    We just started learning contructer and so Im very confused on what to do with Counter(9999).

    Here some of the code I have right now.

    Code:
    # include<iostream>
    using namespace std;
    class Counter
    {
    public:
    	void incr1( int); //increments the units digits by 1
    	void incr10( int); //increments the tens digit by 1
    	void incr100( int);
    	void incr1000( int); //increment the next two digits, respectively
    	void reset();
    	bool overflow();
    	void displayCounter();
    private:
    	int m_cents;
    	int m_dimes;
    	int m_dollars;
    	int m_tens;
    };
    
    void main()
    {
    	Counter clicker;
    	char letter, digit;
    	bool booleanFlag;
    	clicker.reset();
    	cout<<"(c)cents\n(d)dimes\n(u)dollars\n(t)tens\n(o)overflow\n(r)reset\n(e)end\n";
    	cin>>letter;
    	while(letter != 'e' || letter != 'E') 
    	{
    		cout<<"Enter digit 1-9 \n";
    		cin>>digit;
    		switch(letter)
    		{
    			case 'c':
    				clicker.incr1(digit);
    				break;
    			case 'd':
    				clicker.incr10(digit);
    				break;
    			case 'u':
    				clicker.incr100(digit);
    				break;
    			case 't':
    				clicker.incr1000(digit);
    				break;
    			case'o':
    				clicker.overflow();
    				break;
    			case'r':
    				clicker.reset();
    				break;
    		}
    	
    
    		cout<<"(c)cents\n(d)dimes\n(u)dollars\n(t)tens\n(o)overflow\n(r)reset\n(e)end\n";
    		cin>>letter;
    	}
    }
    void Counter::incr1(int digit)
    {
    	m_cents += digit;
    	if(m_cents > 9)
    	{
    		m_cents = m_cents - 9;
    		m_dimes += 1;
    	}
    }
    void Counter::incr10(int digit)
    {
    	m_dimes += digit;
    	if(m_dimes > 9)
    	{
    		m_dimes = m_dimes -9;
    		m_dollars +=1 ;
    	}
    }
    void Counter::incr100(int digit)
    {
    	m_dollars += digit;
    	if(m_dollars > 9)
    	{
    		m_dollars = m_dollars - 9;
    		m_tens += 1;
    	}
    }
    void Counter::incr1000(int digit)
    {
    	m_tens += digit;
    	
    }
    bool Counter::overflow()
    {
    	cout<<"You have overflown counter. Counter has been reset.";
    	reset();
    }
    void Counter::reset() 
    {
    	m_cents = 0;
    	m_dimes = 0;
    	m_dollars = 0;
    	m_tens = 0;
    	total = 0;
    
    }
    I know the code isnt complet, but thats because Im stuck on what to do with the Counter(9999).
    Thanks.
    Last edited by Ganon11; Apr 21 '07, 08:34 PM. Reason: code tags added
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by blackstormdrago n
    Here were our instructions:
    "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. (This was a long time ago.)

    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(units), and tens of dollars. Use the following keys: c, d, u, t, o where c is for cents followed by a digit 1 to 9, d is for dimes followed by a digit 1 to 9, u is for dollars followed by a digit 1 to 9, t is for tens followed by a digit 1 to 9, and o is for overflow. Each entry (one of cdut 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."

    We just started learning contructer and so Im very confused on what to do with Counter(9999).

    Here some of the code I have right now.

    # include<iostrea m>
    using namespace std;
    class Counter
    {
    public:
    void incr1( int); //increments the units digits by 1
    void incr10( int); //increments the tens digit by 1
    void incr100( int);
    void incr1000( int); //increment the next two digits, respectively
    void reset();
    bool overflow();
    void displayCounter( );
    private:
    int m_cents;
    int m_dimes;
    int m_dollars;
    int m_tens;
    };

    void main()
    {
    Counter clicker;
    char letter, digit;
    bool booleanFlag;
    clicker.reset() ;
    cout<<"(c)cents \n(d)dimes\n(u) dollars\n(t)ten s\n(o)overflow\ n(r)reset\n(e)e nd\n";
    cin>>letter;
    while(letter != 'e' || letter != 'E')
    {
    cout<<"Enter digit 1-9 \n";
    cin>>digit;
    switch(letter)
    {
    case 'c':
    clicker.incr1(d igit);
    break;
    case 'd':
    clicker.incr10( digit);
    break;
    case 'u':
    clicker.incr100 (digit);
    break;
    case 't':
    clicker.incr100 0(digit);
    break;
    case'o':
    clicker.overflo w();
    break;
    case'r':
    clicker.reset() ;
    break;
    }


    cout<<"(c)cents \n(d)dimes\n(u) dollars\n(t)ten s\n(o)overflow\ n(r)reset\n(e)e nd\n";
    cin>>letter;
    }
    }
    void Counter::incr1( int digit)
    {
    m_cents += digit;
    if(m_cents > 9)
    {
    m_cents = m_cents - 9;
    m_dimes += 1;
    }
    }
    void Counter::incr10 (int digit)
    {
    m_dimes += digit;
    if(m_dimes > 9)
    {
    m_dimes = m_dimes -9;
    m_dollars +=1 ;
    }
    }
    void Counter::incr10 0(int digit)
    {
    m_dollars += digit;
    if(m_dollars > 9)
    {
    m_dollars = m_dollars - 9;
    m_tens += 1;
    }
    }
    void Counter::incr10 00(int digit)
    {
    m_tens += digit;

    }
    bool Counter::overfl ow()
    {
    cout<<"You have overflown counter. Counter has been reset.";
    reset();
    }
    void Counter::reset( )
    {
    m_cents = 0;
    m_dimes = 0;
    m_dollars = 0;
    m_tens = 0;
    total = 0;

    }

    I know the code isnt complet, but thats because Im stuck on what to do with the Counter(9999).
    Thanks.
    Hi,
    In your incr1000() funtion couldn't you add this:
    Code:
    if (m_tens > 9){
        overflow();}
    and make overflow a void function

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      A Constructor is built like any other function, except that it has no return type and it's name is the same as the class. It should accept 1 argument - the integer denoting the maximum size. To accommodate this, you should have a maxSize variable that is set to this argument. This will be used in the overflow() method. Also, the constructor should set each other member variable to 0 (as the counter starts at 0).

      Basically, a constructor sets up everything within the object so that it is ready to be used. This includes setting any values, initializing variables, instantiating objects, etc.

      Comment

      • blackstormdragon
        New Member
        • Feb 2007
        • 32

        #4
        Thanks ilikepython I almost forgot my overflow there.

        So, Ganon11, something like maxSize = Counter(9999);. Then instead of using reset() at the beginning I should use the Counter(9999) to set everything to zero. The for the overflow I'd have something like if( greater than maxSize){reset( )}

        Hopefully this is what you ment, im going to try it out.

        Thanks

        Comment

        Working...