User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 455,992 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,796 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 896 | Replies: 2
Reply
Join Date: Oct 2007
Posts: 60
Reputation: guitarrick is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
guitarrick guitarrick is offline Offline
Junior Poster in Training

'Grocery Counter' using Classes

  #1  
Dec 6th, 2007
Last project for the semester and it's late!!!
I am in need of some logic with this one....It seemed simple, but now it's out of control. My array with the ASCII converter works, but I can't seem to develop a complete output function to output this 'counter simulator.' Here's the problem and my code thus far:
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 all the items in her basket.
There was a four-digit display, increment buttons for each digit, and a
reset button. There was an overflow indicator that 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 functions void incr1( ); increments the units
digit 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 carry 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
digits 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.

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class GroceryCounter
  5. {
  6. public:
  7. int count;
  8. int Max_count;
  9.  
  10. public:
  11. GroceryCounter();
  12. GroceryCounter(int Max_value);
  13. void reset();
  14. void incr1();
  15. void incr10();
  16. void incr100();
  17. void incr1000();
  18. bool overflow();
  19.  
  20. };
  21.  
  22. GroceryCounter::GroceryCounter()
  23. {
  24. count = 0;
  25. Max_count = 9999;
  26. }
  27.  
  28. GroceryCounter::GroceryCounter(int Max_value)
  29. {
  30. Max_count = Max_value;
  31. count = 0;
  32. }
  33. void GroceryCounter::reset()
  34. {
  35. count = 0;
  36. }
  37.  
  38. void GroceryCounter::incr1()
  39. {
  40. count += 1;
  41. }
  42.  
  43.  
  44.  
  45. void GroceryCounter::incr10()
  46. {
  47. count += 10;
  48. }
  49.  
  50.  
  51.  
  52.  
  53. void GroceryCounter::incr100()
  54. {
  55. count += 100;
  56. }
  57.  
  58. void GroceryCounter::incr1000()
  59. {
  60. count += 1000;
  61. }
  62.  
  63. bool GroceryCounter::overflow()
  64. {
  65. if(count > Max_count)
  66. {
  67. return true;
  68. }
  69. return false;
  70. }
  71.  
  72.  
  73.  
  74.  
  75. /////////////////////////////////////////////////
  76. void main()
  77. {
  78. GroceryCounter countkeeper;
  79. int x = countkeeper.count;
  80. int t = countkeeper.count;
  81. char input[2], choice;
  82.  
  83.  
  84. do
  85. {
  86.  
  87.  
  88. cout << "Welcome to Mom's Grocery Counter" << endl;
  89. cout << "Please enter your monies as follows" << endl;
  90. cout << "Using all lower case letters a,s,d, and f" <<endl;
  91. cout << "followed by the numbers 1-9, use a for cents," << endl;
  92. cout << "s for dimes, d for dollars, and f for tens of dollars." << endl;
  93. cout << "When you choose a letter, follow it with a number, then press enter." << endl;
  94. cin >> input;
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102. int y = (input[1] - '1')+ 1; //ASCII converter to int value
  103.  
  104. int x = 0;
  105. //countkeeper.reset();
  106. switch(input[0])
  107. {
  108.  
  109. case 'a':
  110. for(x = 0; x < y; x++){ countkeeper.incr1(); }
  111. break;
  112. case 's':
  113. for(x = 0; x < y; x++){ countkeeper.incr10(); }
  114. break;
  115. case 'd':
  116. for(x = 0; x < y; x++){ countkeeper.incr100(); }
  117. break;
  118. case 'f':
  119. for(x = 0; x < y; x++){ countkeeper.incr1000(); }
  120. break;
  121.  
  122. default:
  123. break;
  124.  
  125.  
  126. }
  127.  
  128.  
  129.  
  130. cout << "your cents are " << countkeeper.count << endl;
  131. cout << "Please enter y to enter another denomination: " << endl;
  132. cin >> choice;
  133. }while(choice == 'y');
  134.  
Last edited by Ancient Dragon : Dec 6th, 2007 at 12:59 am. Reason: corrected code tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2007
Location: South Dakota
Posts: 980
Reputation: vmanes is a jewel in the rough vmanes is a jewel in the rough vmanes is a jewel in the rough vmanes is a jewel in the rough 
Rep Power: 6
Solved Threads: 97
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Shark

Re: 'Grocery Counter' using Classes

  #2  
Dec 6th, 2007
The assignment does not seem clear as to how display should look. The count should be private, however, so you can't just display that in the main program.

Add a display( ) method that outputs the count in a formatted manner.
You can use division and the modulus operator (%) to divide the count in the the "dollars" and "cents" components, then display them inserting a period between them.

Val
I am in mourning for my country.
Reply With Quote  
Join Date: Oct 2007
Posts: 60
Reputation: guitarrick is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
guitarrick guitarrick is offline Offline
Junior Poster in Training

Re: 'Grocery Counter' using Classes

  #3  
Dec 6th, 2007
...The assignment does not seem clear as to how display should look. The count should be private, however, so you can't just display that in the main program.

Val, thank you. I'll try that later tonight, and I thought so too on the vagary of the display, so I was just looking for some ideas as well on how to do it.
Thanks again,
RG
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the C++ Forum

All times are GMT -4. The time now is 9:36 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC