Answers for "c++ give options"

C++
0

c++ give options

#include <iostream>
#include <iomanip>
using namespace std;

int IntegerOptionsBox(int NumberOfOptions, ...)
{
   //set the border options
   char sczCorners = '+';
   char sczVerticalFillers = '|';
   char sczHorizontalFillers = '-';
   char sczGeneralFillers = ' ';

   // set the spacing options
   int iLineWidth = 40;
   int iOptionSpace = 15;
   int iNumberSpace = 3;
   int iTabSpace = 3;

   int iRightSpace = iLineWidth - iOptionSpace - iNumberSpace - iTabSpace - 1;

   int counter = 0;

   do
   {
      counter++;

      va_list arguments; // A place to store the list of arguments

      va_start(arguments, NumberOfOptions); // Initializing arguments to store all values after NumberOfOptions

      //Top line of the output
      cout << sczCorners << setw(iLineWidth) << setfill(sczHorizontalFillers) << ""
           << sczCorners << "\n";

      //Main Lines of the output
      for (int i = 1; i <= NumberOfOptions; i++)
      {
         cout << sczVerticalFillers;
         cout << setw(iNumberSpace) << setfill(sczGeneralFillers) << right << i << ".";
         cout << setw(iTabSpace) << setfill(sczGeneralFillers) << left << "";
         cout << setw(iOptionSpace) << setfill(sczGeneralFillers) << left << va_arg(arguments, char *);
         cout << setw(iRightSpace) << setfill(sczGeneralFillers) << left << "" << sczVerticalFillers << "\n";
      }

      //Bottom line of the output
      cout << sczCorners << setw(iLineWidth) << setfill(sczHorizontalFillers) << "" << sczCorners << "\n";

      va_end(arguments); // Cleans up the list

      // User's selection process
      int iSelected;
      cout << "\nSelect one of the above options: ";
      cin >> iSelected;
      bool bInputValidated = cin.good(); // Tells if the input is a valid integer or not
      cin.clear();
      cin.ignore(99999, '\n');
      cout << "bInputValidated: " << bInputValidated << endl;
      int iOptionSelected;

      /////////////////////////////
      //      VALIDATION         //
      /////////////////////////////

      if ((bInputValidated == 1)) // if the input is a valid integer
      {
         if ((iSelected <= NumberOfOptions) && (0 < iSelected)) // if the integer is within the range of options
         {
            return iSelected;
         }
         else
         {
            cout << "The option you selected - " << iSelected << " - is not within the range of options. The options range from 1 to " << NumberOfOptions << ". Please try again.\n\n";
         }
      }
      else // the input is not a valid integer
      {
         cout << "The option you selected is not an integer (int) data type. Please try again." << counter << endl
              << endl;
      }

   } while (1);
}




int main() {
  int iOptionSelected = IntegerOptionsBox(2, "Option1", "Option2");

   switch (iOptionSelected)
   {
      case 1:
         cout << "You selected option 1\n";
         break;
   
      case 2:
         cout << "You selected option 2\n";
         break;

      default:
         cout << "This option shouldn't be possible\n";
         break;
   }

}
Posted by: Guest on June-02-2020

Browse Popular Code Answers by Language