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