Hey guys. I know you get questions like these all the time and I tried to look through past posts but I cannot seem to find an example of a hollow square with a menu inside. I have tried many different things and think I need another persons look at it.
A hollow box of "stars" is supposed to surround the menu.
Any information is much appreciated.
#include <iostream>
#include <string>
#include <vector>
usingnamespace std;
void printMenu(vector<string> &vec, unsignedint hpad, unsignedint vpad)
{
unsignedint width = 0;
//find out the length of the longest string
for (vector<string>::iterator it = vec.begin(); it != vec.end(); ++it)
{
if (it->length() > width)
width = it->length();
}
//add the horizontal padding
width += hpad * 2;
//print the top border
cout << string(width + 2, '*') << endl;
//print the vertical padding
for (unsignedint i = 0; i < vpad; ++i)
cout << '*' << string(width, ' ') << '*' << endl;
//print the strings in the menu
for (vector<string>::iterator it = vec.begin(); it != vec.end(); ++it)
{
unsignedint spaces = width - it->length();
unsignedint before = spaces/2;
unsignedint after = spaces - before;
cout << '*' << string(before, ' ') << *it << string(after, ' ') << '*' << endl;
}
//print more vertical padding
for (unsignedint i = 0; i < vpad; ++i)
cout << '*' << string(width, ' ') << '*' << endl;
//print the bottom border
cout << string(width + 2, '*') << endl;
}
int main()
{
vector<string> vstr;
vstr.push_back("Piggy Bank Menu");
vstr.push_back("Current Balance $1000");
vstr.push_back("1) Make Deposit");
vstr.push_back("2) Make Withdrawal");
vstr.push_back("3) View Coins");
vstr.push_back("4) Exit Program");
printMenu(vstr, 4, 1);
return 0;
}
If you were to use it, you would need to do some adjustments because currently it just centers every line. You'd also have to figure out how to put the current balance into a string.