c++ give options
C++
#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;
}
}
Also in C++:
- Title
- c++ functions
- Category
- C++
- Title
- c++ course
- Category
- C++
- Title
- how to cout in c++
- Category
- C++
- Title
- switch statement c++
- Category
- C++
- Title
- switch c++
- Category
- C++
- Title
- factorion
- Category
- C++
- Title
- friend function in c++
- Category
- C++
- Title
- convert stirng to int c++
- Category
- C++
- Title
- flake8 max line length
- Category
- C++
- Title
- c++ cout int
- Category
- C++
- Title
- c++ for loops
- Category
- C++
- Title
- what is time complexity of min_element()
- Category
- C++
- Title
- properties of a set c++
- Category
- C++
- Title
- passing a vector to a function c++
- Category
- C++
- Title
- singleton c++
- Category
- C++
- Title
- nginx linux
- Category
- C++
- Title
- binary tree search
- Category
- C++
- Title
- c++ wait for user input
- Category
- C++
- Title
- double ended queue in c++ stl
- Category
- C++
- Title
- c++ char define
- Category
- C++
- Title
- c++ excel blank cells
- Category
- C++
- Title
- hobo 8
- Category
- C++
- Title
- pop_back
- Category
- C++
- Title
- namespaces c++
- Category
- C++
- Title
- nth_element c++
- Category
- C++
- Title
- c++ convert int to double
- Category
- C++
- Title
- what is difference between ciel and floor
- Category
- C++
- Title
- pass vector by reference c++
- Category
- C++
- Title
- translate
- Category
- C++
- Title
- c++ remove text file
- Category
- C++
- Title
- append string to another string c++
- Category
- C++
- Title
- C++ remove element from set
- Category
- C++
- Title
- how to check type in c++
- Category
- C++
- Title
- create a 2d array c++
- Category
- C++
- Title
- insert elements in array in c++11
- Category
- C++
- Title
- multiple words C++ in same
- Category
- C++
- Title
- calculate factorial
- Category
- C++
- Title
- what are the different ways to traverse a binary tree
- Category
- C++
- Title
- c++ string to integer without stoi
- Category
- C++
- Title
- c++ crash windows
- Category
- C++
- Title
- c++ char to string
- Category
- C++
- Title
- lopping over an array c++
- Category
- C++
- Title
- how to print for limited decimal values in c++
- Category
- C++
- Title
- zeros of array c++
- Category
- C++
- Title
- C++ cin cout
- Category
- C++
- Title
- vector concat c++
- Category
- C++
- Title
- remove item from layout
- Category
- C++
- Title
- checking an int in c++
- Category
- C++
- Title
- new keyword in cpp
- Category
- C++
- Title
- how to format decimal palces in c++
- Category
- C++
- Title
- capitalize first letter c++
- Category
- C++
- Title
- c++ formatting
- Category
- C++
- Title
- c++ show time elapsed
- Category
- C++
- Title
- vector initialization c++
- Category
- C++
- Title
- heredar constructor c++
- Category
- C++
- Title
- insert function in c++ vector
- Category
- C++
- Title
- 2927260.eps 2927262.jpg 2927263.ai License free.txt License premium.txt
- Category
- C++
- Title
- c++ replace n substrings
- Category
- C++
- Title
- set mimetype validation in mongoose
- Category
- C++
- Title
- access last element in vector in c++
- Category
- C++
- Title
- count function c++
- Category
- C++
- Title
- std::reverse
- Category
- C++
- Title
- how to append to a vector c++
- Category
- C++
- Title
- std::substring
- Category
- C++
- Title
- pass by reference c++
- Category
- C++
- Title
- lambda operator in c++
- Category
- C++
- Title
- sort function in c++
- Category
- C++
- Title
- how to print to the serial monitor arduino
- Category
- C++
- Title
- how to delete an element in vector pair in cpp
- Category
- C++
- Title
- c++ char print fixed
- Category
- C++
- Title
- int max in c++
- Category
- C++
- Title
- check an stack is empty c++
- Category
- C++
- Title
- c++ write new file
- Category
- C++
- Title
- how to iterate over unordered_map c++
- Category
- C++
- Title
- c++ movment
- Category
- C++
- Title
- c++ string
- Category
- C++
- Title
- remove element by index from vector c++
- Category
- C++
- Title
- c++ vector
- Category
- C++
- Title
- is TLE means my code is correct but taking more time to computr
- Category
- C++
- Title
- getline in c++
- Category
- C++
- Title
- shuffle vector c++
- Category
- C++
- Title
- sfml basic program
- Category
- C++
- Title
- find number of 1s in a binary cv::mat image
- Category
- C++
- Title
- passing reference in c++
- Category
- C++
- Title
- how to open an input file in c++
- Category
- C++
- Title
- unordered_map c++
- Category
- C++
- Title
- iterative preorder traversal
- Category
- C++
- Title
- what is difference between single inverted and double inverted in programming languages
- Category
- C++
- Title
- c++ remove item from list
- Category
- C++
- Title
- c++ typedef
- Category
- C++
- Title
- level order traversal
- Category
- C++
- Title
- user input c++
- Category
- C++
- Title
- c++ push multiple elements to vector
- Category
- C++
- Title
- typeid to string c++
- Category
- C++
- Title
- c++ try
- Category
- C++
- Title
- c++ map insert
- Category
- C++
- Title
- how to avoid tle in c++
- Category
- C++
- Title
- unordered_set in c++ and ordered set diff
- Category
- C++
- Title
- c++ enum rand
- Category
- C++
- Title
- declaring vector c++
- Category
- C++