make a selection in c++ code example

Example: c++ give options string

string StringOptionsBox(int count, ...)
{
   bool bValidSelection = 1;

   do
   {

      //set the border options
      char sczCorners = '+';
      char sczVerticalFillers = '|';
      char sczHorizontalFillers = '-';
      char sczGeneralFillers = ' ';

      // set the spacing options
      int iLineWidth = 50;
      int iOptionSpace = 25;
      int iNumberSpace = 3;
      int iTabSpace = 3;

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

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

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

      //////////////////////////////////////////////////////////////////////////////////////
      //            LAYOUT THE INTERFACE AND PROMPT THE USER'S SELECTION                  //
      //////////////////////////////////////////////////////////////////////////////////////

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

      //Main Lines of the output
      for (int i = 1; i <= count; 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
      string sSelected;
      cout << "\nSelect one of the above options: ";
	std::getline(cin, sSelected);
      //////////////////////////////////////////////////////////////////////////////////////
      //                   RETRIEVE THE STRING THAT WAS SELECTED                          //
      //////////////////////////////////////////////////////////////////////////////////////

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

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

      //Main Lines of the output
      for (int i = 1; i <= count; i++)
      {
         if (i == atoi(sSelected.c_str()))
         {
            return va_arg(arguments2, char *); // returns the string that was selected
         }

         else if (i != count)
         {
            va_arg(arguments2, char *);
         }
         else
         {
            va_arg(arguments2, char *);
         }
      }
      va_end(arguments2);

      //////////////////////////////////////////////////////////////////////////////////////
      //              MAKE PROVISIONS FOR IF THE SELECTION IS INCORRECT                   //
      //////////////////////////////////////////////////////////////////////////////////////

      cout << "\nERROR \nThe option that you have selected - " << sSelected << " - does not exist. Please try again. \n\n";
      bValidSelection = 0;

   } while (bValidSelection != 1);
}

Tags:

Cpp Example