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

Home Posts Topics Members FAQ

Implement member function Homework

2 New Member
I am working on a homework assignment for my C++ class. Here is the homework description:

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
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. class Counter{
  7.       public:
  8.              void incr1(); //increments the units digits by 1
  9.              void incr10();//increments the tens digit by 1
  10.              void incr100();
  11.              void incr1000();//increment the next two digits, respectively
  12.              void reset();
  13.              bool overflow();
  14.              void displayCounter();
  15.       private:
  16.               int jCents;
  17.               int jDimes;
  18.               int jDollars;
  19.               int jTens;
  20. };
  21.  
  22. int main()
  23. {
  24.     Counter clicker;
  25.     char letter;
  26.     char digit;
  27.     bool booleanFlag;
  28.     clicker.reset();
  29.  
  30.     cout << "Enter A Character Followed By A Digit From 1-9:" << endl;
  31.     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";
  32.     cin >> letter >> digit;
  33.  
  34. // Counter::incr1(int)  
  35.     while(letter != 'Q' || letter != 'q'){
  36.  
  37.                  switch(letter)
  38.                  {
  39.                                case 'a':
  40.                                     clicker.incr1();
  41.                                     break;
  42.                                case 's':
  43.                                     clicker.incr10();
  44.                                     break;
  45.                                case 'd':
  46.                                     clicker.incr100();
  47.                                     break;
  48.                                case 'f':
  49.                                     clicker.incr1000();
  50.                                     break;
  51.                                case 'o':
  52.                                     clicker.overflow();
  53.                                     break;
  54.                                case 'r':
  55.                                     clicker.reset();
  56.                                     break;
  57.                  }
  58.  
  59.     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";
  60.     cin >> letter;
  61. }
  62. }  // end of main
  63.  
  64.  
  65.     void Counter::incr10(){
  66.          jCents += digit;
  67.          if(jCents > 9){
  68.                 jCents = jCents - 9;
  69.                 jDimes += 1;
  70.                 }
  71.          }
  72.  
  73.  
  74.     void Counter::incr1(){
  75.          jCents += digit;
  76.          if(jCents > 9){
  77.                 jCents = 0;
  78.                 jDimes += 1;
  79.                 }
  80.          }
  81.  
  82.  
  83.     void Counter::incr10(){
  84.          jDimess += digit;
  85.          if(jDimess > 9){
  86.                 jDimes = jDimes - 9;
  87.                 jDollars += 1;
  88.                 }
  89.          }
  90.  
  91.  
  92.     void Counter::incr100(){
  93.          jDollars += digit;
  94.          if(jDollarss > 9){
  95.                 jDollars = jDollars - 9;
  96.                 jTens += 1;
  97.                 }
  98.          }
  99.  
  100.     void Counter::incr1000(){
  101.          if (jTens > 9){
  102.                    overflow();
  103.                    }
  104.                    jTens += digit;
  105.          }
  106.  
  107.     bool Counter::overflow(){
  108.          cout << "OVERFLOW HAS OCCURRED. RESULTS ARE NOT RELIABLE. Press Q to quit.";
  109.          reset();
  110.          }
  111.  
  112.     void Counter::reset(){
  113.          jCents = 0;
  114.          jDimes = 0;
  115.          jDollars = 0;
  116.          jTens = 0;
  117.  
  118.          }
  119.  
  120.  
  121.     system("PAUSE");
  122.     return EXIT_SUCCESS;
  123. }
My code is not working for some reason, I have no clue what I did wrong. Could someone please help me figure out what it is I did wrong?

Thanks in advance
Mar 10 '10 #1
4 3383
Banfa
9,065 Recognized Expert Moderator Expert
"My code is not working" is a very poor description of your problem.

What was your observed behaviour of the program?
What was the desired behaviour of the program?
What input did you give the program to get the observed behaviour?
What output did the program give?

1 thing I would say though is that you have made you Counter class overly complex by having separate member variables for cents, dimes dollars and tens when you could use a single variable to just contain the value in cents and you have no member variable to contain the maximum value that can be counted and no constructor to initialise it.

Also line 121 - 123 seem to be the end of main yet your listing indicates main ends at line 62. You can define functions inside other functions so lines 121 - 123 look like you have accidetally left them in or not moved them to line 62.
Mar 10 '10 #2
Jas1936
2 New Member
Ok, I see what you mean. I will move lines 121-123. I know that my class is overly complecated, but that is how my professor told me to do it.

The program is supposed to increment the money type that is selected by the user (ex: dimes). It should increment the amount of times that the user selects(ex: dimes, 9, should equal 90 cents)

When I ran my program it listed the options, but it did not increment the counter, and it did not reset.

I entered in "S 2" for dimes, incremented twice, which shuold have been 20 cents.

The program just started over from the beginning without displaying the "0.20" that was expected.
Mar 10 '10 #3
Banfa
9,065 Recognized Expert Moderator Expert
I think you must have done more than just more those lines. The way that the posted code uses digit would produce compiler errors.

digit is local to main so you class methods can not use it. Main should be calling the relevant incr* function digit times but main actually ignores the value of digit having read it in.

The posted main function never outputs the value so that is why you are not seeing it.

Finally in the posted code each incr* function trys to handle overflow by itself, when jDimess overflows incr10 increments jDollars itself. However that does not handle overflow in jDollars, it would be better to call incr100 which does handle overflow in jDollars. And the same for the other class methods.
Mar 10 '10 #4
weaknessforcats
9,208 Recognized Expert Moderator Expert
I suggest you keep the money in pennies. That is, 1234 is the same as $12.34 and I'm sure you can convert 1234 to $12.34:

dollars = money/100
cents = money % 100

So display a $ then display dollars then display a period then display cents. And you are done.

If you need nickels:

nickels = money/5
cents = money % 5

etc...
Mar 10 '10 #5

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

Similar topics

26
by: Oplec | last post by:
Hi, I am learning standard C++ as a hobby. The C++ Programming Language : Special Edition has been the principal source for my information. I read the entirety of the book and concluded that I...
13
by: John Leslie | last post by:
Hi, If I have: struct foof { long x; }; void blarg(void) {
3
by: Sean | last post by:
Hi all, I wonder what are the possible ways to implement a Map in C++? Among these methods, which is the best one and why? Thank you very much in advance. Sean
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
11
by: Manuel | last post by:
Hi, I need implement a map of member functions of some class. This map is formed by a string and a pointer to the member function. The problem is that the map need that the object saved are...
7
by: sunny | last post by:
Hai, can anyone implemen this problem.I am unable to implement.i tried but i am going out of the design pattern. Implement the Factory Design Pattern for the following scenario: Base class =...
52
by: Ben Voigt [C++ MVP] | last post by:
I get C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27): error CS0535: 'UselessJunkForDissassembly.InvocableInternals' does not implement interface member...
3
by: v4vijayakumar | last post by:
While we can do whatever we want to do with the member variables, with the member references, why member references are not common? Note: This is not homework question. :)
5
by: ravi | last post by:
Can any body tell me How to implement a stack using two queues Thax in advance
5
by: xz | last post by:
I am coding for this little class Date, which represents the date consisting of year, month and day. The header file is as follows: #ifndef DATE_H #define DATE_H class Date { static const...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
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,...
0
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
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.