Adding border

So I'm trying to add a border around my menu. Someone told me to try using screenloc() and box(). However, they are undefined in my code. I'm not familiar with it and surprisingly can't find much on either. Any recommendations? Also, please bare with me, I'm new to this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "stdafx.h"
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <Windows.h>
#include <string>
#include <iostream>

void screenloc(int x, int y);
void box(int x, int l, int b);

using namespace std;

class account
{

	int balance;
	int add;//deposit
	int sub;//withdraw
	int checkAcct = 0;//checking account balance
	int savAcct = 0;//saving account balance
	int t;//transfer
	float c_bal = 0;//checking account balance
	float s_bal = 0;//savings account balance
	float x;//deposit
	char name[50];

public:
	void addAcct();
	void deposit();
	void withdrawal();
	void fundsTransfer();
	void acctInfo();
	void transctionHis();
};
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>

std::size_t max_width( const std::vector<std::string>& items )
{
    std::size_t maxw = 0 ;
    for( const auto& str : items ) if( str.size() > maxw ) maxw = str.size() ;
    return maxw ;
}

std::vector<std::string> box( std::vector<std::string> items, char box_char = '#' )
{
    const std::size_t BORDER_SIZE = 4 ;
    const std::size_t width = max_width(items) + BORDER_SIZE ;
    const std::string prefix { box_char, ' ' } ;
    const std::string suffix { ' ', box_char } ;
    const std::string line_one( width, box_char) ;
    const std::string line_two = prefix + std::string( width-BORDER_SIZE, ' ' ) + suffix ;

    std::vector<std::string> boxed { line_one, line_two } ;
    for( auto& str : items )
    {
        str.resize( width-BORDER_SIZE, ' ' ) ;
        boxed.push_back( prefix + str + suffix ) ;
        boxed.push_back(line_two) ;
    }
    boxed.push_back(line_one) ;
    return boxed ;
}

void print_menu( const std::vector<std::string>& items )
{
    for( const auto& line : box(items) ) std::cout << line << '\n' ;
    std::cout << "\nenter choice: " ;
}

int main()
{
    print_menu( { "1. make coffee", "2. listen to jazz", "3. take a stroll", "4. sleep" } ) ;
}

http://coliru.stacked-crooked.com/a/7a2b6bc86615a5f8
Thanks. Sadly understand bout 5% off your awesome code.
Also, I realized it would probably be better to put it around the actual menu.

1
2
3
4
5
6
7
8
9
		printf("*==Main Menu==*\n");
		printf("*1. Create New Account*\n");
		printf("*2. Cash Deposit (Savings)*\n");
		printf("*3. Cash Withdrawl (Savings)*\n");
		printf("*4. Transfer Funds (Savings to Checking)*\n");
		printf("*5. Account information*\n");
		printf("*6. Transaction information*\n");
		printf("*7. Exit*\n\n");
		printf("*Press a choice between the range [1-7]*\n\n");
Last edited on
jaxsmurph wrote:
Sadly understand bout 5% off your awesome code.

You are in good company :-)

jaxsmurph wrote:
Also, I realized it would probably be better to put it around the actual menu.

What JLBorges gave you is a general solution that you can easily adjust to your needs.
You only need to invoke the function “print_menu” adding as parameter a vector of strings which are the items of your menu.

I’m adding you an example (I didn’t touch JLBorges’s code, but I moved their functions below main(), in the order they are invoked – hope it can help you following the chain of invokations):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>
#include <vector>

void print_menu( const std::vector<std::string>& items );
std::vector<std::string> box( std::vector<std::string> items, char box_char = '#' );
std::size_t max_width( const std::vector<std::string>& items );
void waitForEnter();

int main()
{
    std::vector<std::string> mymenu;

    mymenu.push_back("==Main Menu==");
    mymenu.push_back("1. Create New Account");
    mymenu.push_back("2. Cash Deposit (Savings)");
    mymenu.push_back("3. Cash Withdrawl (Savings)");
    mymenu.push_back("4. Transfer Funds (Savings to Checking)");
    mymenu.push_back("5. Account information");
    mymenu.push_back("6. Transaction information");
    mymenu.push_back("7. Exit");
    mymenu.push_back("Press a choice between the range [1-7]");

    print_menu( mymenu ) ;

    waitForEnter();
    return 0;
}

void print_menu( const std::vector<std::string>& items )
{
    for( const auto& line : box(items) ) std::cout << line << '\n' ;
    std::cout << "\nenter choice: " ;
}

std::vector<std::string> box( std::vector<std::string> items, char box_char )
{
    const std::size_t BORDER_SIZE = 4 ;
    const std::size_t width = max_width(items) + BORDER_SIZE ;
    const std::string prefix { box_char, ' ' } ;
    const std::string suffix { ' ', box_char } ;
    const std::string line_one( width, box_char) ;
    const std::string line_two = prefix + std::string( width-BORDER_SIZE, ' ' ) + suffix ;

    std::vector<std::string> boxed { line_one, line_two } ;
    for( auto& str : items )
    {
        str.resize( width-BORDER_SIZE, ' ' ) ;
        boxed.push_back( prefix + str + suffix ) ;
        boxed.push_back(line_two) ;
    }
    boxed.push_back(line_one) ;
    return boxed ;
}

std::size_t max_width( const std::vector<std::string>& items )
{
    std::size_t maxw = 0 ;
    for( const auto& str : items ) if( str.size() > maxw ) maxw = str.size() ;
    return maxw ;
}

void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

@jaxsmurph

Here's a menu with borders, using a Box function I created years ago. I have it to center itself on screen, using the longest line in the menu array, but it can be placed wherever you wish, by changing the screenloc(across,down).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Adding Menu Borders.cpp : main project file.

#include <iomanip>
#include <iostream>
#include <limits>
#include <string>
#include <vector>
#include <windows.h>

using std::cout;
using std::cin;
using std::endl;
using std::string;

using namespace System;

HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;

enum {
	black,          //  0
	dark_blue,      //  1
	dark_green,     //  2
	dark_cyan,      //  3
	dark_red,       //  4
	dark_magenta,   //  5
	dark_yellow,    //  6
	light_gray,     //  7
	dark_gray,      //  8
	light_blue,     //  9
	light_green,    // 10
	light_cyan,     // 11
	light_red,      // 12
	light_magenta,  // 13
	light_yellow,   // 14
	white           // 15
};

#define on , // So I can use the function - void text(text_color on background_color)
// To more easily remember which is text color vs background color

void print_menu( string mymenu[9], int len );
void screenloc(int x, int y);
void Box(int style, int across, int down, int amount, int rows, int b_color, int f_color, int shadow, int shadow_bc, int shadow_fc);


// My text color function. Use it if you wish.
void text(int text_color = 7 on int paper_color = 0)
{
	// defaults to light_gray on black

	int color_total = (text_color + (paper_color * 16));
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color_total);
}

int main()
{
	string mymenu[]={"==Main Menu==","1. Create New Account","2. Cash Deposit (Savings)","3. Cash Withdrawl (Savings)",
		"4. Transfer Funds (Savings to Checking)","5. Account information","6. Transaction information",
		"7. Exit"," ","Press a choice between the range [1-7]"};
	int len = 0;

	for(int x=0;x<10;x++)
	{
		if(mymenu[x].length() > len)
			len = mymenu[x].length();// Find length of longest line
		     // to center menu across screen width
	}
	Box(1,1,1,118,28,black,light_gray,0,0,0);
	Box(2,(120-len)/2,4,len+4,13,black,light_cyan,1,light_gray,dark_gray);
	print_menu( mymenu, len ) ;
	return 0;
}

void print_menu( string mymenu[9],int len )
{
	int x,choice;
	screenloc((120-mymenu[0].length())/2+2,5);
	cout << mymenu[0];
	for( x=1;x<10;x++)
	{
		screenloc((120-len)/2+2,6+x);
		cout << mymenu[x];
	}
	do{
		text(black on light_gray);
		screenloc(52,20);
		cout << "Enter choice:  \b" ;
		cin >> choice;
	}while (choice < 1 || choice > 7);
	if(choice>0 && choice <7)
	{
		screenloc(45,22);
		cout << "You chose " << mymenu[choice];
	}
	if(choice == 7)
	{
		screenloc(45,22);
		cout << "Good-bye for now!!";
	}
text(black on light_gray);
screenloc(5,27);

}

void screenloc(int x, int y)
{
	CursorPosition.X = x;
	CursorPosition.Y = y;
	SetConsoleCursorPosition(console, CursorPosition);
}

void Box(int style, int across, int down, int amount, int rows, int f_color, int b_color, int shadow, int shadow_fc, int shadow_bc)
{

	char Shadow[4] = { ' ', '\xB0', '\xB1', '\xB2' };
	char  Style[5][11] = {
		{ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
		{ ' ', '\xDA', '\xC4', '\xBF', '\xB3', '\xC0', '\xD9', '\xC3', '\xB4', '\xC2', '\xC1' },
		{ ' ', '\xC9', '\xCD', '\xBB', '\xBA', '\xC8', '\xBC', '\xCC', '\xB9', '\xCB', '\xCA' },
		{ ' ', '\xD5', '\xCD', '\xB8', '\xB3', '\xD4', '\xBE', '\xC6', '\xB5', '\xD1', '\xCF' },
		{ ' ', '\xD6', '\xC4', '\xB7', '\xBA', '\xD3', '\xBD', '\xC7', '\xB6', '\xD2', '\xD0' }
	};

	int x;
	string BoxLine(amount - 2, Style[style][2]);
	string BoxBody(amount - 2, ' ');
	string ShadowLine(amount, Shadow[shadow]);

	screenloc(across, down);
	text(f_color on b_color);
	cout << Style[style][1] << BoxLine << Style[style][3];
	for (x = 1; x < rows; x++)
	{
		screenloc(across, down + x);
		cout << Style[style][4] << BoxBody << Style[style][4];
		if (shadow)
		{
			text(shadow_fc on shadow_bc);
			cout << Shadow[shadow];
			text(f_color on b_color);
		}
	}
	screenloc(across, down + rows - 1);
	cout << Style[style][5] << BoxLine << Style[style][6];
	if (shadow)
	{
		text(shadow_fc on shadow_bc);
		cout << Shadow[shadow];
		screenloc(across + 1, down + rows);
		cout << ShadowLine;
		text(f_color on b_color);
	}
}
closed account (48T7M4Gy)
For simple jobs it's often easier to hardcode the menu as follows;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>

int main()
{
    // ALTERNATIVE 1
    std::cout
    << "***********************************************\n"
    << "*             == Main Menu ==                 *\n"
    << "* 1. Create New Account                       *\n"
    << "* 2. Cash Deposit (Savings)                   *\n"
    << "* 3. Cash Withdrawl (Savings)                 *\n"
    << "* 4. Transfer Funds (Savings to Checking)     *\n"
    << "* 5. Account information                      *\n"
    << "* 6. Transaction information                  *\n"
    << "* 7. Exit                                     *\n"
    << "*                                             *\n"
    << "*    Press a choice between the range [1-7]   *\n"
    << "***********************************************\n";
    
    
    // ALTERNATIVE 2
    std::string menu =
    "***********************************************\n"
    "*             == Main Menu ==                 *\n"
    "* 1. Create New Account                       *\n"
    "* 2. Cash Deposit (Savings)                   *\n"
    "* 3. Cash Withdrawl (Savings)                 *\n"
    "* 4. Transfer Funds (Savings to Checking)     *\n"
    "* 5. Account information                      *\n"
    "* 6. Transaction information                  *\n"
    "* 7. Exit                                     *\n"
    "*                                             *\n"
    "*    Press a choice between the range [1-7]   *\n"
    "***********************************************\n";
    
    std::cout << menu;
    
    return 0;
}


If you need to re-use the menu then just make it a string as per alternative 2.
Topic archived. No new replies allowed.