c++ give options code example
Example: c++ give options
#include <iostream>
#include <iomanip>
using namespace std;
int IntegerOptionsBox(int NumberOfOptions, ...)
{
char sczCorners = '+';
char sczVerticalFillers = '|';
char sczHorizontalFillers = '-';
char sczGeneralFillers = ' ';
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;
va_start(arguments, NumberOfOptions);
cout << sczCorners << setw(iLineWidth) << setfill(sczHorizontalFillers) << ""
<< sczCorners << "\n";
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";
}
cout << sczCorners << setw(iLineWidth) << setfill(sczHorizontalFillers) << "" << sczCorners << "\n";
va_end(arguments);
int iSelected;
cout << "\nSelect one of the above options: ";
cin >> iSelected;
bool bInputValidated = cin.good();
cin.clear();
cin.ignore(99999, '\n');
cout << "bInputValidated: " << bInputValidated << endl;
int iOptionSelected;
if ((bInputValidated == 1))
{
if ((iSelected <= NumberOfOptions) && (0 < iSelected))
{
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
{
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;
}
}