#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::cerr;
using std::cin;
using std::endl;

// set which characters we'll for yes/no
const char YES = 'Y';
const char NO = 'N';

// set the maximum number of input characters to clear on broken input
const int LineLen = 80;

 // how many estimates we'll go through in our square root calculations
const int AccuracyIterations = 10;

// asks the user if they want to skip a section (Y or N)
//    repeats the question (using do while) until they enter Y or y or N or n
// returns true if they want to skip, false otherwise
bool skipThis(string section);

// stepOne uses a while loop to get a number greater than one from the user then returns it
// (repeats until a valid value is supplied)
float stepOne();

// stepTwo uses a for loop to approximate the square root of a number
// (each pass through the loop gets a better approximation than the previous pass,
//  stepTwo uses stepOne to get the number)
void stepTwo();

int main()
{
   // run the two parts, giving the user chances to skip either or both
   if (!skipThis("part one by itself")) {
      float positiveNum;
      positiveNum = stepOne();
      cout << "Your number was " << positiveNum << endl;
      cout << "End of part one" << endl;
   }
   if (!skipThis("part two")) {
      stepTwo();
   }
}

bool skipThis(string section)
{
   char skipOrNot;
   bool validAnswer = false;
   do {

      // get the user's choice
      cout << "Do you want to skip " << section << " (" << YES << " or " << NO << "): ";
      cin >> skipOrNot;

      // convert their response to uppercase (in case they answered y or n not Y or N)
      skipOrNot = toupper(skipOrNot);

      // see if it was a YES, a NO, or something invalid
      if (skipOrNot == YES) {
         return true;
      } else if (skipOrNot == NO) {
         validAnswer = true;
      } else {
         cout << "That was not a valid response, please try again" << endl;
      }

   } while (!validAnswer);

   // we got out of the loop because they said No, so they don't want to skip
   return false;
}

float stepOne()
{
    float finalNum; // the user-selected value
    bool validNumber = false; // remembers if we have a good answer yet (or not)

    // keep going as long as we don't yet have a good answer
    while (!validNumber) {
       // try to get a positive number from the user
       cout << "Please enter a number greater than 1.0: ";
       cin >> finalNum;

       // check for non-numeric input and clean up if that's the case
       if (cin.fail()) {
          cin.clear();
          cin.ignore(LineLen, '\n');
          cerr << "That was not a number, please try again" << endl;
       }

       // check for too-small values
       else if (finalNum <= 1) {
          cerr << "That was <= 1, please try again" << endl;
       }

       // if the value was ok then change ValidNumber to true (so we'll get out of the loop)
       else {
          validNumber = true;
       }
    }

    // since we got out of the loop we must have a good answer
    return finalNum;
}

void stepTwo()
{
   // get the number we'll estimate the square root of
   float originalNum = stepOne();

   // keep track of our current estimate and its square
   float estimate, square;

   // keep track of lower and upper bounds on our estimate
   // (we know the real square root is somewhere between these)
   float lowerBound = 1;
   float upperBound  = originalNum;

   // go through the correct number of estimates
   int estimateNum;
   for (estimateNum = 1; estimateNum <= AccuracyIterations; estimateNum++) {
       // guess halfway between the lower/upper bounds we have so far,
       // display that as our next estimate
       estimate = (lowerBound + upperBound)/2;
       cout << "Guess number " << estimateNum << ": " << estimate << endl;

       // see what the square of our estimate is
       square = estimate * estimate;

       // if it is too big then make the estimate our new upper bound
       if (square > originalNum) {
          upperBound = estimate;
       }

       // otherwise if it is too small then make the estimate our new lower bound
       else if (square < originalNum) {
          lowerBound = estimate;
       }

       // otherwise we got it exactly, leave the loop early
       else {
          cout << "We got it exactly in " << estimateNum << " tries!" << endl;
          break;
       }
   }

   cout << "Our final estimate was " << estimate << ", whose square is ";
   cout << square << " (aiming for " << originalNum << ")" << endl << endl;
}

