By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,559 Members | 1,154 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,559 IT Pros & Developers. It's quick & easy.

Grocery Store counter

blackstormdragon
P: 32
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.

Expand|Select|Wrap|Line Numbers
  1. # include<iostream>
  2. using namespace std;
  3. class Counter
  4. {
  5. public:
  6.     void incr1( int); //increments the units digits by 1
  7.     void incr10( int); //increments the tens digit by 1
  8.     void incr100( int);
  9.     void incr1000( int); //increment the next two digits, respectively
  10.     void reset();
  11.     bool overflow();
  12.     void displayCounter();
  13. private:
  14.     int m_cents;
  15.     int m_dimes;
  16.     int m_dollars;
  17.     int m_tens;
  18. };
  19.  
  20. void main()
  21. {
  22.     Counter clicker;
  23.     char letter, digit;
  24.     bool booleanFlag;
  25.     clicker.reset();
  26.     cout<<"(c)cents\n(d)dimes\n(u)dollars\n(t)tens\n(o)overflow\n(r)reset\n(e)end\n";
  27.     cin>>letter;
  28.     while(letter != 'e' || letter != 'E') 
  29.     {
  30.         cout<<"Enter digit 1-9 \n";
  31.         cin>>digit;
  32.         switch(letter)
  33.         {
  34.             case 'c':
  35.                 clicker.incr1(digit);
  36.                 break;
  37.             case 'd':
  38.                 clicker.incr10(digit);
  39.                 break;
  40.             case 'u':
  41.                 clicker.incr100(digit);
  42.                 break;
  43.             case 't':
  44.                 clicker.incr1000(digit);
  45.                 break;
  46.             case'o':
  47.                 clicker.overflow();
  48.                 break;
  49.             case'r':
  50.                 clicker.reset();
  51.                 break;
  52.         }
  53.  
  54.  
  55.         cout<<"(c)cents\n(d)dimes\n(u)dollars\n(t)tens\n(o)overflow\n(r)reset\n(e)end\n";
  56.         cin>>letter;
  57.     }
  58. }
  59. void Counter::incr1(int digit)
  60. {
  61.     m_cents += digit;
  62.     if(m_cents > 9)
  63.     {
  64.         m_cents = m_cents - 9;
  65.         m_dimes += 1;
  66.     }
  67. }
  68. void Counter::incr10(int digit)
  69. {
  70.     m_dimes += digit;
  71.     if(m_dimes > 9)
  72.     {
  73.         m_dimes = m_dimes -9;
  74.         m_dollars +=1 ;
  75.     }
  76. }
  77. void Counter::incr100(int digit)
  78. {
  79.     m_dollars += digit;
  80.     if(m_dollars > 9)
  81.     {
  82.         m_dollars = m_dollars - 9;
  83.         m_tens += 1;
  84.     }
  85. }
  86. void Counter::incr1000(int digit)
  87. {
  88.     m_tens += digit;
  89.  
  90. }
  91. bool Counter::overflow()
  92. {
  93.     cout<<"You have overflown counter. Counter has been reset.";
  94.     reset();
  95. }
  96. void Counter::reset() 
  97. {
  98.     m_cents = 0;
  99.     m_dimes = 0;
  100.     m_dollars = 0;
  101.     m_tens = 0;
  102.     total = 0;
  103.  
  104. }
I know the code isnt complet, but thats because Im stuck on what to do with the Counter(9999).
Thanks.
Apr 21 '07 #1
Share this Question
Share on Google+
3 Replies


ilikepython
Expert 100+
P: 844
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<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.
Hi,
In your incr1000() funtion couldn't you add this:
Expand|Select|Wrap|Line Numbers
  1. if (m_tens > 9){
  2.     overflow();}
  3.  
and make overflow a void function
Apr 21 '07 #2

Ganon11
Expert 2.5K+
P: 3,652
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.
Apr 21 '07 #3

blackstormdragon
P: 32
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
Apr 21 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.