473,494 Members | 2,223 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Grocery Store counter

blackstormdragon
32 New Member
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
3 10113
ilikepython
844 Recognized Expert Contributor
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
3,652 Recognized Expert Specialist
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
32 New Member
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

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

Similar topics

5
4206
by: pdb | last post by:
I want to make something so I can make a grocery list easy and just print it off. I am guessing I would have a drop down list, which would be a list of tables, then select item from the table. For...
1
1378
by: J.J. Feminella | last post by:
I have an unfinished small class hierarchy that looks something like the code snippet below. The intent is to have a simple company identification system that ensures uniqueness by calling...
4
1793
by: Mark S. | last post by:
Hello, On a high volume page we have the following JavaScript: img = new Image() img.src = 'http://myserver.com/count.aspx?x=1'; and it works fine, but now we've added: img.onload =...
3
2121
by: gordon | last post by:
Hi I am looking to store some details about a user's configuration choices, in particular the place where they have installed some data files, the OS that they use, and their Windows user name. ...
5
2069
by: jasonchan | last post by:
How would you set up a counter in javascript that goes from 5 to 0 This is what i have so far and it is not displaying in the div container "counter" for some reason... <!DOCTYPE html PUBLIC...
7
2362
by: Pim75 | last post by:
Hello, I want to store multiple records at once in a SQL database with a For..Next instruction like the sample code below: For counter = 0 To 100 Dim dbInsert As New SqlCommand( _ "INSERT...
4
10216
by: Billy | last post by:
I'm in need of a grocery database complete with generic data. I've been looking for some time now to build my web site's database with this but no success finding it. I need something like a...
0
1227
by: JuAn2226 | last post by:
hi this my code Private Sub Form_Load() car_count = 0 Cumulative_Speed = 0 End Sub Private Sub Timer1_Timer() Dim tmpNumber As Integer
4
2237
by: ndedhia1 | last post by:
I am reading in a file to see what delays I am getting on what IP address: QuoteBlockTiming exceeded 1000 ms: 1684 --- Fri Nov 06 06:09:10 CST 2009 170.137.94.95 Class key = 649126730 block...
0
6989
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7195
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5453
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4889
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4579
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1400
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
644
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
285
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.