// basic.cpp solution for lab3, CSCI 159 fall 2026
// Author: D. Wessels

// only the iostream library is needed (basic input/output only)
#include <iostream>
using std::cin;
using std::cout;
using std::cerr;
using std::endl;

// max number of chars to skip on bad input
const int LineLen = 80;

// stepOne associates an approximate mark with a grade of 0-100
void stepOne();

// stepTwo checks for non-numeric input, allowing a retry if invalid
void stepTwo();

// stepThree uses recursion to repeatedly get/check a positive numeric value,
// continuing until something valid is provided
void stepThree();

int main()
{
   stepOne();
   stepTwo();
   stepThree();
   // if we used the int stepThree() in the "Tip for moving on to lab3.cpp"
   // int userVal = stepThree();
}

void stepOne()
{
   cout << endl << "This is step one" << endl;

   // get the user's score
   float score;
   cout << "Please enter your test score (0-100): ";
   cin >> score;

   // check for fail
   if (score < 49.5) {
      cout << "Sorry, but that is an F grade" << endl;
   }

   // check for A+
   else if (score >= 89.5) {
      cout << "Congrats, that is an A+ grade" << endl;
   }

   // give a message for anything in between
   else {
      cout << "That was somewhere in the D to A range" << endl;
   }
}

void stepTwo()
{
   cout << endl << "This is step two" << endl;

   // get a positive number (greater than zero) from the user
   float posNum;
   cout << "Please enter a number greater than zero, e.g. 17.123: ";
   cin >> posNum;

   // catch non-numeric input, give the user an error message, and clean up the input buffer
   if (cin.fail()) {
      cerr << "That was not a number, please only enter numbers" << endl;
      cerr << "Please try again" << endl;
      cin.clear(); // clear the error state
      cin.ignore(LineLen, '\n'); // clear the bad input data
      cin >> posNum;
   }

   // catch too-small values and try again
   else if (posNum <= 0) {
      cerr << "That was not a positive number, please enter numbers ? 0" << endl;
      cerr << "Please try again" << endl;
      cin >> posNum;
   }

   // display their answer back to them
   cout << "You entered " << posNum << endl;
}

void stepThree()
{
   cout << endl << "This is step three" << endl;

   // get a positive number (greater than zero) from the user
   int score;
   const int MinScore = 1;
   const int MaxScore = 100;
   cout << "Please enter an integer in range " << MinScore << " to " << MaxScore << ": ";
   cin >> score;

   // catch non-numeric input, give the user an error message, and clean up the input buffer
   if (cin.fail()) {
      cerr << "Sorry, only integer input is accepted, please try again" << endl;
      cin.clear(); // clear the error state
      cin.ignore(LineLen, '\n'); // clear the bad input data
      stepThree();
   }

   // catch too-small/too-large values and try again
   else if (score < MinScore) {
      cerr << "Sorry, your value (" << score << ") was too small, please try again" << endl;
      stepThree();
   }

   else if (score > MaxScore) {
      cerr << "Sorry, your value (" << score << ") was too large, please try again" << endl;
      stepThree();
   }

   // otherwise they got it right this time, display their answer back to them
   else {
      cout << "You selected a valid value " << score << endl << endl;
   }
}
