//
//
//  winning.cc
//
//      AUTHOR: Russell Lowke
//         T.F: Francis Crick
//  CSCIE-s111: Unit Three, Main - question 7 [worth 15pts],  7/8/2002
//
// Description: read in a sequence of chars that represent whether a baseball
//				team won or lost each game and prints out percentage of wins to
//              the nearest tenth of a percent, along with the length of the
//				longest winning streak and the longest losing streak
//				In input data, 'w' or 'W' represents a win, 'l' or 'L'
//				represents a loss.
//
//       Usage: When prompted key in a line of 'l's and/or 'w's representing play
//
//      Method: main calls two primary functions
//              getInput()     - reads input from user, a string of chars
//              printData()    - prints baseball data
//
//              fnctns
//              setDecPlace()  - sets cout to display using [percision] decimal places
//
//       Notes: 
//

	
#include <iostream>
#include <iomanip>

const int EMPTY = 0;
const int WIN   = 1;
const int LOSE  = 2;


//
//  set cout to display up to [percision] decimal places
//
void setDecPlace(int percision)
{
	// "formula" for precision notation
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(percision);
}


//
//   Baseball class
//   Collects data about baseball games, particularily
//   number of wins, number of losses, winning streaks and losing streaks
//
class Baseball
{
     private :
     
		int  pWins,						// number of wins
			 pLosses,					// number of losses
			 pWinStreak,				// best winning streak
			 pLoseStreak;				// worst losing streak
			 
			 
       public:
       
       		 Baseball();				// constructor
       		 ~Baseball();				// destructor
		void getInput();				// input for baseball
		void printData();				// print baseball data
		
}; 


//
//  constructor for Baseball class
//  
Baseball :: Baseball()
{
	pWins       = 0;					// clear all properties
	pLosses     = 0;
	pWinStreak  = 0;
	pLoseStreak = 0;
}


//
//  destructor for Baseball class
//  
Baseball :: ~Baseball()
{
}


//
//  read baseball input char by char, searching for
//  wins (W) and lose (L), tallying these into pWinStreak or
//  pLoseStreak respectively.  Also keep track of longest running
//  streak [streak], by checking the [lastResult]
//
void Baseball :: getInput()
{
	char c;
	int lastResult = EMPTY;				// lastResult must being EMPTY
	int streak = 1;
	
	cin.unsetf(ios::skipws);			// unset skip whitespace
	
	cout << "Baseball game input: ";
	
	//  read input char by char, tallying results into win and loss
	do {
	
		cin >> c;						// get input from keyboard
		
		switch (c) {
			case 'w': case 'W':			// win input
				pWins++;
				
				if (lastResult == WIN) {
					streak++;
					if (streak > pWinStreak) {	// new win streak
						pWinStreak = streak;
					}
				} else {
					streak = 1;			// reset streak
				}
				lastResult = WIN;
				
				break;
				
			case 'l': case 'L':			// lose input
				pLosses++;
				
				if (lastResult == LOSE) {
					streak++;
					if (streak > pLoseStreak) {	// new lose streak
						pLoseStreak = streak;
					}
				} else {
					streak = 1;			// reset streak
				}
				lastResult = LOSE;
				
				break;
		}
	} while ( c != '\n' );				// loop until newline
}


//
//  Print data for Baseball 
//
void Baseball :: printData()
{	
	int    totalGames = pWins + pLosses;
	double percentWon = ( (pWins / (float) totalGames) * 100 );
	
	setDecPlace(2);						// 2 dec places for output
	
	cout << "Percentage of games won: " << percentWon  << "%" << endl;
	cout << "Longest winning streak: "  << pWinStreak  << endl;
	cout << "Longest losing streak: "   << pLoseStreak << endl;
}


//
//  construct Baseball object
//  get input for Baseball game
//  print baseball data
//
void main()
{
	Baseball game;						// create game
	
	game.getInput();					// read input
	game.printData();					// print data
	
	exit(0);
}
