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.

#include <iostream>
using namespace std;

class GroceryCounter
{
public:
	int count;
	int Max_count;

public:
	GroceryCounter();
	GroceryCounter(int Max_value);
	void reset();
	void incr1();
	void incr10();
	void incr100();
	void incr1000();
	bool overflow();

};

GroceryCounter::GroceryCounter()
{
	count = 0;
	Max_count = 9999;
}

GroceryCounter::GroceryCounter(int Max_value)
{
	Max_count = Max_value;
	count = 0;
}
void GroceryCounter::reset()
{
	count = 0;
}

void GroceryCounter::incr1()
{
	count += 1;
}



void GroceryCounter::incr10()
{
	count += 10;
}




void GroceryCounter::incr100()
{
	count += 100;
}

void GroceryCounter::incr1000()
{
	count += 1000;
}

bool GroceryCounter::overflow()
{
	if(count > Max_count)
	{
		return true;
	}
	return false;
}




/////////////////////////////////////////////////
void main()
{
	GroceryCounter countkeeper;
	int x = countkeeper.count;
    int t = countkeeper.count;
	char input[2], choice;


do
	{

	
	cout << "Welcome to Mom's Grocery Counter" << endl;
	cout << "Please enter your monies as follows" << endl;
	cout << "Using all lower case letters a,s,d, and f" <<endl;
	cout << "followed by the numbers 1-9, use a for cents," << endl;
	cout << "s for dimes, d for dollars, and f for tens of dollars." << endl;
	cout << "When you choose a letter, follow it with a number, then press enter." << endl;
	cin >>  input;
	



	

	
	int y = (input[1] - '1')+ 1; //ASCII converter to int value
	
	int x = 0;
	//countkeeper.reset();
	switch(input[0])
	{
	
	case 'a':
		for(x = 0; x < y; x++){ countkeeper.incr1(); }
		break;
	case 's':
		for(x = 0; x < y; x++){ countkeeper.incr10(); }
		break;
	case 'd':
		for(x = 0; x < y; x++){ countkeeper.incr100(); }
		break;
	case 'f':
		for(x = 0; x < y; x++){ countkeeper.incr1000(); }
		break;
		
		default:
		break;

		
	}


	
    cout << "your cents are " << countkeeper.count << endl;
	cout << "Please enter y to enter another denomination: " << endl;
	cin >> choice;
	}while(choice == 'y');

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

...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

This article has been dead for over six months. Start a new discussion instead.