//
//
//  increase.cc
//
//      AUTHOR: Russell Lowke
//         T.F: Francis Crick
//  CSCIE-s111: Unit Two, Main - question 10 [worth 8pts],  7/3/2002
//
//
// Description: Computes the largest increase in stock over NUMDAYS days
//
//       Usage: When prompted, key in stock value for day
//
//      Method: main calls on three functions
//              readStock()    - reads stock values from user, keeping track of highest
//                               and previous values
//              printLargest() - prints the highest change in stock value
//
//                ...for all user input a special function s used
//              getInt()       - reads an integer from keyboard, checking for errors
//
//       Notes: Properties for this program are stored as globals
//              as we haven't covered classes/properties/methods yet!
//


#include <iostream>
#include <iomanip>


// constants
const int NUMDAYS = 10;

// globals
//    ... these should be properties in a class, but we haven't covered classes yet!
//
int largestIncrease      = 0,
	dayOfIncrease        = 0,
	stockOnDayOfIncrease = 0;

//
//  reads an integer from the keyboard,
//  uses error checking to ensure value is between minValue and maxValue
//
int getInt(char askStr[], int minValue, int maxValue)
{
	int mVar;
	
	while (1) { // ...while TRUE
	
		// ask query
		cout << askStr;
		cin >> mVar;
		
		if ( mVar >= minValue && mVar <= maxValue )
			break;  // input OK, break from loop
		else {
			cout << "Bad input: please enter a value between "
			     << minValue << " and " << maxValue << endl;
			cin.clear();
			cin.ignore(80, '\n');
		}
	}
	
	return mVar;
}


void readStock()
{
	int prevStockPrice = 0,
	    stockPrice     = 0,
	    changeInStock  = 0;
	
	// loop through NUMDAYS
	for (int i = 1; i <= NUMDAYS; i++) {
		
		// get stock input for day
		cout << "Day: " << i;
		stockPrice = getInt("   Stock Price : ", 0, 100);
		
		// assign prevStockPrice if on first day
		if (i == 1)
			prevStockPrice = stockPrice;
		
		// calc. the change in stock
		changeInStock = stockPrice - prevStockPrice;
		
		// test if stock increase > largest increase yet
		if (changeInStock > largestIncrease) {
		
			// store data for this day
			largestIncrease = changeInStock;
			dayOfIncrease = i;
			stockOnDayOfIncrease = stockPrice;
		}
		
		// assign prevStockPrice before moving to next day
		prevStockPrice = stockPrice;
	}
}


void printLargest()
{
	if (largestIncrease == 0)   // no increase
		cout << "there was no increase in stock over the " 
		     << NUMDAYS << " days." << endl;
	else {
		cout << "Largest Increase of " << largestIncrease << endl;
	     
		cout << "  from " << (stockOnDayOfIncrease - largestIncrease) 
	   	     << " to " << stockOnDayOfIncrease << endl;
	     
		cout << "  occured between day #" << (dayOfIncrease - 1) 
	     	 << " and day #" << dayOfIncrease << "." << endl;
	}
}


void main()
{
    readStock();
    printLargest();
}