473,386 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Grocery Store counter

blackstormdragon
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 10101
ilikepython
844 Expert 512MB
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 Expert 2GB
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
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
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
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
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
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
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
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
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
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
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...

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.