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

Absolute C++ counter problem

P: 6
There is a programming project that I just don't know how to implement. Here is what it is asking:

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.

How would I implement this idea into C++ code? just some hints, I can't figure this one out, any hint would be greatly appreciated. If anyone has the book and can help me, I would be very thankful

Thanks in advance.
Feb 21 '07 #1
Share this Question
Share on Google+
10 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
There is a programming project that I just don't know how to implement. Here is what it is asking:

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.

How would I implement this idea into C++ code? just some hints, I can't figure this one out, any hint would be greatly appreciated. If anyone has the book and can help me, I would be very thankful

Thanks in advance.
So it seems you need a number for the total, an indicator if you have overflowed or not, and a test to see if you have overflowed.

If you put those things into a loop that exited on a sentinel (possibly overflow, possibly a character), you'd have it.

Is there something specific you are having trouble with?
Feb 21 '07 #2

P: 6
The problem is that I need to make this using a class Counter (sorry I omitted that) and using at least two constructors. I am a beginner in this, but I am really interested in knowing classes, I just don't know how to implement this problem into a program.
Feb 22 '07 #3

sicarie
Expert Mod 2.5K+
P: 4,677
The problem is that I need to make this using a class Counter (sorry I omitted that) and using at least two constructors. I am a beginner in this, but I am really interested in knowing classes, I just don't know how to implement this problem into a program.
Oh, well that's pretty easy to do as well - those attributes are just going to go into the class (just not the loop).

Are you having difficulty with the concept of a class, or is the "two constructors" thing throwing you (or even something else)?
Feb 22 '07 #4

P: 6
I wanted to use a shorter idea on this, but it seems I left it out a whole bunch of vital info in order for you to help me. Here is the rest of the problem:

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 void100(); and void1000(); increments 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.

Thank you for your help.
Feb 22 '07 #5

P: 6
Oh, well that's pretty easy to do as well - those attributes are just going to go into the class (just not the loop).

Are you having difficulty with the concept of a class, or is the "two constructors" thing throwing you (or even something else)?
Yep that is the problem, I have started with C++ about 2 months ago, I am a graphics designer, but live in a city where finding a job in that field is like finding a needle in a haystack. I understand the idea of class, just don't know the implementation, the theory, the logic of it that stumbles me, same goes for the constructors, I understand what they do, just having trouble understanding their logic.

Thank you for your input and help.
Feb 22 '07 #6

sicarie
Expert Mod 2.5K+
P: 4,677
Yep that is the problem, I have started with C++ about 2 months ago, I am a graphics designer, but live in a city where finding a job in that field is like finding a needle in a haystack. I understand the idea of class, just don't know the implementation, the theory, the logic of it that stumbles me, same goes for the constructors, I understand what they do, just having trouble understanding their logic.

Thank you for your input and help.
I really like this site - it's got good material and shows little examples, but this is a good readup on classes, with a bit of code to help.
Feb 22 '07 #7

P: 6
So I tried and basically it is lack of knowledge on my part, but how can I start eh class? This is really alien to me and an example of how this one would look would be greatly appreciated. Thanks in advance, but I feel like this is really important and I have no one to help in this field, I am learning this myself.
Feb 27 '07 #8

P: 6
Oh and also, this is how much I have actually done, but it doesn't work. Maybe something I am doing wrong.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream> 
  2. using namespace std;
  3.  
  4. class Counter {
  5. public:
  6.     Counter(int cents, int dimes, int dollars, int tensDollars);
  7.  
  8.     void reset();
  9.     void incr1(),
  10.          incr10(),
  11.          incr100(),
  12.          incr1000();
  13.     bool overflow();
  14.  
  15. };
  16.  
  17. int main() {
  18.     Counter(9, 9, 9, 9);
  19.     cout << "Please enter the amount of cents, dimes, dollars and tens of dollars \n";
  20.     cout << "by using keys a (for cents), s (for dimes), d (for dollars), f (for tens dollars) \n";
  21.     cout << "and o (for overflow) followed by numbers 1-9 to specify the amount of cents, dimes etc. \n";
  22.     cout << "Please enter amount of cents:\n";
  23.     cin >> money.incr1;
  24.     cout << "Please enter amount of dimes:\n";
  25.     cin >> money.incr10;
  26.     cout << "Please enter amount of dollars:\n";
  27.     cin >> money.incr100;
  28.     cout << "Please enter amount of tens pf dollars:\n";
  29.     cin >> money.incr1000;
  30.  
  31.     return 0;
  32. }
Feb 27 '07 #9

Ganon11
Expert 2.5K+
P: 3,652
How is it not working? Are there errors during compilation? Is it compiling but giving unexpected results?
Feb 27 '07 #10

sicarie
Expert Mod 2.5K+
P: 4,677
How is it not working? Are there errors during compilation? Is it compiling but giving unexpected results?
I'm going to guess errors - he declares "Counter" instead of "money" in his main, the functions he uses money.increment are not called using parentheses, and there are no definitions of what those methods do in his class.

captainMarkis - you've got a good start - the right structure and ideas, there are just the finer points that are missing. This is a good tutorial/reference for classes - declaring and implementing. I like it because it usually comes with examples that are clear and concise and illustrate the code.

Other than that, and declaring your variables not only in the constructor, but also the class, you need to define what those increment methods do in your class. It's clear to the reader, but the compiler only sees that as an identifier - you can name them vodka(), tonic(), and martini() for all the compiler cares...

Check out that tutorial and let us know if it gives you any more trouble.
Feb 27 '07 #11

Post your reply

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