//File: useful.cpp
//Implementation of the functions described in useful.h

#include <iostream>
#include <cassert>
#include <climits>
#include <cmath>
#include <iomanip>

using std::cin;
using std::cout;
using std::endl;
using std::fixed;
using std::showpoint;
using std::setprecision;
using std::setw;
using std::setfill;
using std::right;

#include "useful.h"

namespace RohnA{

int GetInteger(const char* prompt, int min, int max)
{
    assert(min < max);

    int number;
    for(;;)
    {
        cout << prompt << ": ";
        cin >> number;

        if (cin.fail())
        {
            cout << "Please enter an integer." << endl;
            cin.clear();
            cin.ignore(1024,'\n');
        }
        else if (cin.peek() != '\n')
        {
            cout << "Please enter only an integer." << endl;
            cin.ignore(1024,'\n');
        }
        else if (number < min || number > max)
        {
            cout << "Please enter a number between " <<
                min << " and " << max << "." << endl;
            cin.ignore(1024,'\n');
        }
        else
        {
            cin.ignore(1024,'\n');
            return number;
        }
    }
}


double GetDouble(const char* prompt)
{
    double number;
    for(;;)
    {
        cout << prompt << ": ";
        cin >> number;

        if (cin.fail())
        {
            cout << "Please enter a number." << endl;
            cin.clear();
            cin.ignore(1024,'\n');
        }

        else if (cin.peek() != '\n')
        {
            cout << "Please enter only a number." << endl;
            cin.ignore(1024,'\n');
        }

        else
        {
            cin.ignore(1024,'\n');
            return number;
        }
    }
}


double GetDouble(const char* prompt, double min, double max)
{
    assert(min < max);

    double number;
    for(;;)
    {
        cout << prompt << ": ";
        cin >> number;

        if (cin.fail())
        {
            cout << "Please enter a number." << endl;
            cin.clear();
            cin.ignore(1024,'\n');
        }

        else if (cin.peek() != '\n')
        {
            cout << "Please enter only a number." << endl;
            cin.ignore(1024,'\n');
        }

        else if (number < min || number > max)
        {
            cout << "Please enter a number between " <<
                min << " and " << max << "." << endl;
            cin.ignore(1024,'\n');
        }

        else
        {
            cin.ignore(1024,'\n');
            return number;
        }
    }
}


char GetChar(const char* prompt)
{
	char input;
    for(;;)
    {
        cout << prompt << ": ";
        cin >> input;

        if (cin.fail())
        {
            cout << "Please enter a character." << endl;
            cin.clear();
            cin.ignore(1024,'\n');
        }

		else if (cin.peek() != '\n')
        {
            cout << "Please enter only a character." << endl;
            cin.ignore(1024,'\n');
        }

        else
        {
            cin.ignore(1024,'\n');
            return input;
        }
    }
}


bool Ask(const char* prompt)
{
	char response;
	for(;;)
	{
		cout << prompt << "? (Y/N)";
		cin >> response;

		if(cin.fail() || cin.peek() != '\n' ||
		  (response != 'Y' && response != 'y' && response != 'N' && response != 'n'))
		{
			cout << "\nThat was an invalid response.\n";
			cin.clear();	
			cin.ignore(1024,'\n');	
		}				
		else
		{
			cin.ignore(1024,'\n');	
			return (response == 'Y' || response == 'y') ? true : false;
		}
	}
}


void DisplayDollar(double amount)
{
	cout << '$' << fixed << showpoint << setprecision(2) << right << setw(8) << setfill('*') << amount;
	return;
}


double Distance(double x1, double y1, double x2, double y2)
{
	return sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
}


}//end namespace
