All pastes #10751 Raw Edit

Untitled

public text v1 · immutable
#10751 ·published 2005-05-02 03:17 UTC
rendered paste body

	

#include <conio.h>

//int     getch(void);


#include <iostream>
#include <cctype>
using namespace std;

class CCounter 
{
private:
  int f_Counter;
  bool f_OverflowFlag;
  bool overflow() 
  {
    return f_OverflowFlag;
  }
  void increment(int inc) {
    int newCounter = f_Counter + inc;
    // if smaller - int limits overflow
    if(newCounter < f_Counter ||
     // if MaxValue is not -1, check for its overflow
       (f_MaxValue != -1 && newCounter > f_MaxValue)) 
	   {
         f_OverflowFlag = true;
         newCounter = newCounter - f_MaxValue;
		}
       f_Counter = newCounter;
  }
public:
  CCounter(int max) : f_MaxValue(max), f_Counter(0), f_OverflowFlag(false)
  {}
  CCounter() : f_MaxValue(9999), f_Counter(0), f_OverflowFlag(false) // no max value
  {}
  ~CCounter() { }
  
  int f_MaxValue;

  void reset() 
  {
    f_Counter = 0;
    f_OverflowFlag = false;
  }
  void incrcents() 
  {
    increment(1);
  }
  void incrdimes() 
  {
    increment(10);
  }
  void incrdollars() 
  {
    increment(100);
  }
  void incrtendollars() 
  {
    increment(1000);
  }

  void reportoverflow() 
  {
    if(overflow())
      cout << "Overflow" << endl;
    else
      cout << "No Overflow" << endl;
  }
  void currentamount() 
  {
    cout << f_Counter/100 << " dollars and " << f_Counter%100 << " cents. " << endl;
  }
};

void getkeys();
void showtitle();

int main()
{
  showtitle();
  getkeys();

  return 0;

}//end main

void showtitle()
{
	cout << "***********************************\n";
	cout << "* Mom's Grocerie Counter Emulator *\n";
	cout << "***********************************\n";
	cout << endl;
	cout << "The counter is currently active.  To increase the number\n";
	cout << "of dollars in increments of ten, press and release the 't'\n";
	cout << "key.  To raise dollars in one dollar increments, press the\n";
	cout << "'o' key.  Pressing the 'd' key will increment the dimes of\n";
	cout << "the dollar amount.  The 'p' key will increment pennies.\n";
	cout << "The 'v' key will display the current value on the counter.\n";
	cout << "\nTo exit the program, press the \"q\" key." << endl << endl << endl; 
}

void getkeys()
{
	CCounter asdf;
	char key;
    key = getch();
  while ((key != 'q') || (key != 'Q')) 
	{
  	switch (key)
		{
	  	case 'p':
	  	  asdf.incrcents();
		  break;
	  	case 'd':
		  asdf.incrdimes();
		  break;
		case 'o':
		  asdf.incrdollars();
	  	  break;
		case 't':
		  asdf.incrtendollars();
		  break;
	  	case 'v':
	  	  asdf.currentamount();
		  break;
		default: 
		  cout << "Invalid entry.\n";
		}
	  key = getch();
	}
}