"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.
Expand|Select|Wrap|Line Numbers
- # 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;
- }
Thanks.