有没有办法在已建立的 for 循环框 (C++) 中创建菜单
Is there a way to create a menu inside an established for loop box (C++)
我对学习 C++ 还是很陌生,看不出如何在我使用 ASCII 创建的另一个 for 循环框中添加一个“for 循环菜单”。
我正在尝试使用已构建框内的数组创建一个包含 7 个选项的菜单。以我有限的知识,我认为我需要为数组添加另一个 for 循环,因为我已经知道选项的数量?尝试在 for 循环中使用 for 循环是我错了吗?
对于我应该在哪里放置 for 循环菜单有什么建议吗?或者有没有更有效的方法将菜单添加到框中?
这是我为盒子打印的代码副本:
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
// This is a loop structure for the menu box using ASCII
// This prints the top left corner of the menu box (ASCII)
for (int row = 0; row < 1; ++row) {
cout << char(201);
}
// This prints the top border of the menu box (ASCII)
for (int column = 0; column < 80; ++column) {
cout << char(205);
}
// This prints the top right corner of the menu box (ASCII)
for (int row = 0; row < 1; ++row) {
cout << char(187);
}
cout << "\n";
// This prints the left border of the menu box (ASCII)
for (int row = 0; row < 20; ++row) {
cout << char(186);
// This prints spaces inside the menu box
for (int column = 0; column < 80; ++column) {
cout << " ";
}
// This prints the right border of the menu box (ASCII)
cout << char(186) << "\n";
}
// This will print the bottom left corner of the menu box (ASCII)
for (int row = 0; row < 1; ++row) {
cout << char(200);
}
// This prints the bottom border of the menu box (ASCII)
for (int column = 0; column < 80; ++column) {
cout << char(205);
}
// This prints the bottom right corner of the menu box (ASCII)
for (int row = 0; row < 1; ++row) {
cout << char(188);
}
cout << "\n";
system("pause");
return 0;
}
使用您选择的方法,您需要添加一个包含菜单选项的数组并打印它们而不是菜单框内的空格。要使用菜单选项保存对齐方式,您可以使用 <iomanip>
.
中的 setw()
结果代码可能如下所示:
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
using namespace std;
int main()
{
const int menuWidth = 80;
// This is a loop structure for the menu box using ASCII
// This prints the top left corner of the menu box (ASCII)
cout << char(201);
// This prints the top border of the menu box (ASCII)
for (int column = 0; column < menuWidth; ++column) {
cout << char(205);
}
// This prints the top right corner of the menu box (ASCII)
cout << char(187);
cout << "\n";
// array with menu options
const string menuOptions[] = { "Option1", "Option2", "Option3", "Option4", "Option5", "Option6", "Option7" };
for (string option : menuOptions)
{
cout << char(186) // print left border
<< setw(menuWidth) // set next item width
<< left // set next item aligment
<< option // print menu option string with width and aligment
<< char(186) << "\n"; // print right border
}
// This will print the bottom left corner of the menu box (ASCII)
cout << char(200);
// This prints the bottom border of the menu box (ASCII)
for (int column = 0; column < menuWidth; ++column) {
cout << char(205);
}
// This prints the bottom right corner of the menu box (ASCII)
cout << char(188);
cout << "\n";
system("pause");
return 0;
}
此外,您可以尝试缩进,或在菜单的顶部或底部添加空行以使其看起来更好看。
关于您的代码的几点说明:
- 您不需要使用
for
循环来打印一个符号(如角) c:
- 经常使用的常量值(比如菜单的宽度)最好保存在常量中
我对学习 C++ 还是很陌生,看不出如何在我使用 ASCII 创建的另一个 for 循环框中添加一个“for 循环菜单”。
我正在尝试使用已构建框内的数组创建一个包含 7 个选项的菜单。以我有限的知识,我认为我需要为数组添加另一个 for 循环,因为我已经知道选项的数量?尝试在 for 循环中使用 for 循环是我错了吗?
对于我应该在哪里放置 for 循环菜单有什么建议吗?或者有没有更有效的方法将菜单添加到框中?
这是我为盒子打印的代码副本:
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
// This is a loop structure for the menu box using ASCII
// This prints the top left corner of the menu box (ASCII)
for (int row = 0; row < 1; ++row) {
cout << char(201);
}
// This prints the top border of the menu box (ASCII)
for (int column = 0; column < 80; ++column) {
cout << char(205);
}
// This prints the top right corner of the menu box (ASCII)
for (int row = 0; row < 1; ++row) {
cout << char(187);
}
cout << "\n";
// This prints the left border of the menu box (ASCII)
for (int row = 0; row < 20; ++row) {
cout << char(186);
// This prints spaces inside the menu box
for (int column = 0; column < 80; ++column) {
cout << " ";
}
// This prints the right border of the menu box (ASCII)
cout << char(186) << "\n";
}
// This will print the bottom left corner of the menu box (ASCII)
for (int row = 0; row < 1; ++row) {
cout << char(200);
}
// This prints the bottom border of the menu box (ASCII)
for (int column = 0; column < 80; ++column) {
cout << char(205);
}
// This prints the bottom right corner of the menu box (ASCII)
for (int row = 0; row < 1; ++row) {
cout << char(188);
}
cout << "\n";
system("pause");
return 0;
}
使用您选择的方法,您需要添加一个包含菜单选项的数组并打印它们而不是菜单框内的空格。要使用菜单选项保存对齐方式,您可以使用 <iomanip>
.
结果代码可能如下所示:
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
using namespace std;
int main()
{
const int menuWidth = 80;
// This is a loop structure for the menu box using ASCII
// This prints the top left corner of the menu box (ASCII)
cout << char(201);
// This prints the top border of the menu box (ASCII)
for (int column = 0; column < menuWidth; ++column) {
cout << char(205);
}
// This prints the top right corner of the menu box (ASCII)
cout << char(187);
cout << "\n";
// array with menu options
const string menuOptions[] = { "Option1", "Option2", "Option3", "Option4", "Option5", "Option6", "Option7" };
for (string option : menuOptions)
{
cout << char(186) // print left border
<< setw(menuWidth) // set next item width
<< left // set next item aligment
<< option // print menu option string with width and aligment
<< char(186) << "\n"; // print right border
}
// This will print the bottom left corner of the menu box (ASCII)
cout << char(200);
// This prints the bottom border of the menu box (ASCII)
for (int column = 0; column < menuWidth; ++column) {
cout << char(205);
}
// This prints the bottom right corner of the menu box (ASCII)
cout << char(188);
cout << "\n";
system("pause");
return 0;
}
此外,您可以尝试缩进,或在菜单的顶部或底部添加空行以使其看起来更好看。
关于您的代码的几点说明:
- 您不需要使用
for
循环来打印一个符号(如角) c: - 经常使用的常量值(比如菜单的宽度)最好保存在常量中