7.3 My mother always took a little red counter to the grocery store . The counte
ID: 3773667 • Letter: 7
Question
7.3 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 default constructor should create a Counter object that can count up to 9999 and have a value of 0. The object should have a public member function void setLimit(int ) that resets the counting limit from 1 to 9999. When it is called it should call the reset function. If setCounter's argument is less then or equal to 0 or greater then 9999 the mutator should throw an exception.
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 created by the default constructor or setCounter. Overflow should be called after any series of calls to any of the incrementor functions. If overflow occurs the counter limit should be reported, and the fact that the counter has been reset to 0 by calling the reset function.
Use this class to provide a simulation of my mother’s little red clicker. The clicker is represented by an integer on the screen. 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 but no special formatting is to be done to represent this on the screen. The class should have a member functions: int getCounterand int getLimit that when called returns the current value of the clicker and the current limit. These functions should be called by the main program after the clicker's limit is set, when it is incremented , reset, or overflow happens.
Provide keys for cents, dimes, dollars, tens of dollars, and reset. Use the keys asdfrq: a for cents; i for dimes; d for dollars; f for tens of dollars; r for the reset function; s to set counter and q to quit. Once a letter is selected and return is pressed the user is asked to enter a digit between 1 and 9 . The appropriate member function, based on whether asdf was pressed, should be called 1 to 9 times toincrement the counter.
ORIGINAL QUESTION BELOW
7.3 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 con-
structed 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.
SAMPLE RUN #3
Interactive Session
Enter a limit for the counter (1-9999):-22
You've tried to set counter to an illegal value.
Setting counter to a limit of 9999 by default.
The current value of the counter is: 0 and the limit of the counter is: 9999
Enter (a) for cents; (i) for dimes; (d) for dollars; (f) for tens of dollars;
(r) for the reset function; (s) to set counter and (q) to quit:r
The current value of the counter is: 0 and the limit of the counter is: 9999
Enter (a) for cents; (i) for dimes; (d) for dollars; (f) for tens of dollars;
(r) for the reset function; (s) to set counter and (q) to quit:s
Enter a limit for the counter (1-9999):2000
The current value of the counter is: 0 and the limit of the counter is: 2000
Enter (a) for cents; (i) for dimes; (d) for dollars; (f) for tens of dollars;
(r) for the reset function; (s) to set counter and (q) to quit:f
Enter (1-9) to increment:9
You've exceeded 2000 which is the limit of the counter by 7000. Counter being reset to 0.
The current value of the counter is: 0 and the limit of the counter is: 2000
Enter (a) for cents; (i) for dimes; (d) for dollars; (f) for tens of dollars;
(r) for the reset function; (s) to set counter and (q) to quit:f
Enter (1-9) to increment:1
The current value of the counter is: 1000 and the limit of the counter is: 2000
Enter (a) for cents; (i) for dimes; (d) for dollars; (f) for tens of dollars;
(r) for the reset function; (s) to set counter and (q) to quit:s
Enter a limit for the counter (1-9999):9000
The current value of the counter is: 0 and the limit of the counter is: 9000
Enter (a) for cents; (i) for dimes; (d) for dollars; (f) for tens of dollars;
(r) for the reset function; (s) to set counter and (q) to quit:i
Enter (1-9) to increment:-22
Enter (1-9) to increment (you entered an illegal number) :3
The current value of the counter is: 30 and the limit of the counter is: 9000
Enter (a) for cents; (i) for dimes; (d) for dollars; (f) for tens of dollars;
(r) for the reset function; (s) to set counter and (q) to quit:q
Explanation / Answer
Counter.cpp
#include<iostream>
#include<math.h>
#include<stdlib.h>
using namespace std;
class Counter
{
public:
Counter(int max);
void incr1(int x);
void incr10(int x);
void incr100(int x);
void incr1000(int x);
bool overflow();
void reset();
int getCounter();
bool over;
void setLimit(int n);
int getLimit();
private:
int num;
int max;
};
bool keyIsCorrect(char key);
bool keyIsCorrect(char key)
{
if (key == 'a') return true;
if (key == 'i') return true;
if (key == 's') return true;
if (key == 'd') return true;
if (key == 'f') return true;
if (key == 'o') return true;
if (key == 'r') return true;
if (key == 'q') return true;
cout << "key = " << key << endl;
return false;
}
Counter::Counter(int n)
{
num = 0;
if (n > 0 && n <= 9999)
{
max = n;
}
else
{
cout << "You've tried to set counter to an illegal value." << endl;
cout << "Setting counter to a limit of 9999 by default." << endl;
max = 9999;
}
over = false;
return;
}
void Counter::incr1(int x)
{
if (num + x >= max)
over = true;
else
num = num + x;
return;
}
void Counter::incr10(int x)
{
if (num + 10 * x >= max)
over = true;
else
num = num + 10 * x;
return;
}
void Counter::incr100(int x)
{
if (num + 100 * x >= max)
over = true;
else
num = num + 100 * x;
return;
}
void Counter::incr1000(int x)
{
if (num + 1000 * x >= max)
over = true;
else
num = num + 1000 * x;
return;
}
bool Counter::overflow()
{
return over;
}
void Counter::reset()
{
over = false;
num = 0;
return;
}
void Counter::setLimit(int n)
{
reset();
if (n > 0 && n <= 9999)
{
max = n;
}
else
{
cout << "You've tried to set counter to an illegal value." << endl;
cout << "Setting counter to a limit of 9999 by default.";
max = 9999;
}
}
int Counter::getCounter()
{
return num;
}
int Counter::getLimit()
{
return max;
}
int main()
{
char key;
char dummy;
int n = 0;
cout << "Enter a limit for the counter (1-9999): ";
cin >> n;
Counter counter(n);
cout << "Current value of counter is :" << counter.getCounter() << "and the limit of the counter is: " << counter.getLimit() << endl;
cout << "Enter " << endl;
cout << "a for cents " << endl;
cout << "i for dimes " << endl;
cout << "d for dollars " << endl;
cout << "f for tens of dollars " << endl;
cout << "o for overflow can be requested by pressing " << endl;
cout << "r for reset function " << endl;
cout << "s to set counter" << endl;
cout << "q to quit:" << endl;
cin >> key;
while (keyIsCorrect(key))
{
if (key == 'a')
{
int x;
cout << "Enter(1 - 9) to increment : ";
cin >> x;
while (x <= 0)
{
cout << "Enter(1 - 9) to increment(you entered an illegal number) : ";
cin >> x;
}
counter.incr1(x);
if (counter.overflow())
{
counter.reset();
cout << "You've exceeded" << counter.getLimit() << "which is the limit of the counter by " << (counter.getCounter() + x) - counter.getLimit()
<< "Counter being reset to 0." << endl;
}
}
else if (key == 'i')
{
int x;
cout << "Enter(1 - 9) to increment : ";
cin >> x;
while (x <= 0)
{
cout << "Enter(1 - 9) to increment(you entered an illegal number) : ";
cin >> x;
}
counter.incr10(x);
if (counter.overflow())
{
counter.reset();
cout << "You've exceeded" << counter.getLimit() << "which is the limit of the counter by " << (counter.getCounter() + 10 * x) - counter.getLimit()
<< "Counter being reset to 0." << endl;
}
}
else if (key == 'd')
{
int x;
cout << "Enter(1 - 9) to increment : ";
cin >> x;
while (x <= 0)
{
cout << "Enter(1 - 9) to increment(you entered an illegal number) : ";
cin >> x;
}
counter.incr100(x);
if (counter.overflow())
{
counter.reset();
cout << "You've exceeded" << counter.getLimit() << "which is the limit of the counter by " << (counter.getCounter() + 100 * x) - counter.getLimit()
<< "Counter being reset to 0." << endl;
}
}
else if (key == 'f')
{
int x;
cout << "Enter(1 - 9) to increment : ";
cin >> x;
while (x <= 0)
{
cout << "Enter(1 - 9) to increment(you entered an illegal number) : ";
cin >> x;
}
counter.incr1000(x);
if (counter.overflow())
{
counter.reset();
cout << "You've exceeded " << counter.getLimit() << " which is the limit of the counter by " << (counter.getCounter()+1000*x) - counter.getLimit()
<< " Counter being reset to 0." << endl;
}
}
else if (key == 's')
{
cout << "Enter a limit for the counter (1-9999): ";
int limit;
cin >> limit;
counter.setLimit(limit);
}
else if (key == 'r')
{
counter.reset();
}
else if (key == 'q')
{
exit(1);
}
cout << "Current value of counter is :" << counter.getCounter() << " and the limit of the counter is: " << counter.getLimit() << endl;
cout << "Enter ";
cout << "a for cents " << endl;
cout << "i for dimes " << endl;
cout << "d for dollars " << endl;
cout << "f for tens of dollars " << endl;
cout << "o for overflow can be requested by pressing " << endl;
cout << "r for reset function " << endl;
cout << "s to set counter" << endl;
cout << "q to quit:" << endl;
cin >> key;
cout << endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.