c 2b 2b give options

Solutions on MaxInterview for c 2b 2b give options by the best coders in the world

showing results for - "c 2b 2b give options"
Ivanna
12 Nov 2017
1#include <iostream>
2#include <iomanip>
3using namespace std;
4
5int IntegerOptionsBox(int NumberOfOptions, ...)
6{
7   //set the border options
8   char sczCorners = '+';
9   char sczVerticalFillers = '|';
10   char sczHorizontalFillers = '-';
11   char sczGeneralFillers = ' ';
12
13   // set the spacing options
14   int iLineWidth = 40;
15   int iOptionSpace = 15;
16   int iNumberSpace = 3;
17   int iTabSpace = 3;
18
19   int iRightSpace = iLineWidth - iOptionSpace - iNumberSpace - iTabSpace - 1;
20
21   int counter = 0;
22
23   do
24   {
25      counter++;
26
27      va_list arguments; // A place to store the list of arguments
28
29      va_start(arguments, NumberOfOptions); // Initializing arguments to store all values after NumberOfOptions
30
31      //Top line of the output
32      cout << sczCorners << setw(iLineWidth) << setfill(sczHorizontalFillers) << ""
33           << sczCorners << "\n";
34
35      //Main Lines of the output
36      for (int i = 1; i <= NumberOfOptions; i++)
37      {
38         cout << sczVerticalFillers;
39         cout << setw(iNumberSpace) << setfill(sczGeneralFillers) << right << i << ".";
40         cout << setw(iTabSpace) << setfill(sczGeneralFillers) << left << "";
41         cout << setw(iOptionSpace) << setfill(sczGeneralFillers) << left << va_arg(arguments, char *);
42         cout << setw(iRightSpace) << setfill(sczGeneralFillers) << left << "" << sczVerticalFillers << "\n";
43      }
44
45      //Bottom line of the output
46      cout << sczCorners << setw(iLineWidth) << setfill(sczHorizontalFillers) << "" << sczCorners << "\n";
47
48      va_end(arguments); // Cleans up the list
49
50      // User's selection process
51      int iSelected;
52      cout << "\nSelect one of the above options: ";
53      cin >> iSelected;
54      bool bInputValidated = cin.good(); // Tells if the input is a valid integer or not
55      cin.clear();
56      cin.ignore(99999, '\n');
57      cout << "bInputValidated: " << bInputValidated << endl;
58      int iOptionSelected;
59
60      /////////////////////////////
61      //      VALIDATION         //
62      /////////////////////////////
63
64      if ((bInputValidated == 1)) // if the input is a valid integer
65      {
66         if ((iSelected <= NumberOfOptions) && (0 < iSelected)) // if the integer is within the range of options
67         {
68            return iSelected;
69         }
70         else
71         {
72            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";
73         }
74      }
75      else // the input is not a valid integer
76      {
77         cout << "The option you selected is not an integer (int) data type. Please try again." << counter << endl
78              << endl;
79      }
80
81   } while (1);
82}
83
84
85
86
87int main() {
88  int iOptionSelected = IntegerOptionsBox(2, "Option1", "Option2");
89
90   switch (iOptionSelected)
91   {
92      case 1:
93         cout << "You selected option 1\n";
94         break;
95   
96      case 2:
97         cout << "You selected option 2\n";
98         break;
99
100      default:
101         cout << "This option shouldn't be possible\n";
102         break;
103   }
104
105}