当 运行 on Windows 或 Linux 时函数表现不同
Function behaves differently when run on Windows or Linux
我有一个简单的功能,可以将文本行打印到控制台,居中,空的 space 填充有“=”符号。当我 运行 我的程序在 Linux 上使用此功能时,我看到在控制台顶部正确显示的文本 window 随后是我程序的菜单提示,但是在 Windows 它不打印任何内容并直接跳到菜单提示。这两个程序都是使用 GNU gcc 和 -std=c++11.
在代码块中编译和 运行
void _print_center(vector<string>& tocenter)
{
int center;
for ( int x; x<static_cast<int>(tocenter.size());x++ )
{
char sfill = '=';
string line = tocenter[x];
center = (68/2)-(tocenter[x].length()/2);
line.replace(0, 0, center, sfill);
cout << std::left << std::setfill(sfill);
cout << std::setw(68) << line << endl;
}
}
您的问题得到了答案(未初始化的变量)。我建议您理清并简化您的代码,这样这类问题就不会经常出现。例如:
创建一个以单个字符串为中心的函数。
void center( std::ostream& os, const std::string& text, int width ) {
if ( text.size() >= width ) {
// Nothing to center, just print the text.
os << text << std::endl;
} else {
// Total whitespace to pad.
auto to_pad = width - text.size();
// Pad half on the left
auto left_padding = to_pad / 2;
// And half on the right (account for uneven numbers)
auto right_padding = to_pad - left_padding;
// Print the concatenated strings. The string constructor will
// correctly handle a padding of zero (it will print zero `=`).
os << std::string( left_padding, '=' )
<< text
<< std::string( right_padding, '=' )
<< std::endl;
}
}
一旦您测试该函数对单个字符串运行良好,依靠 C++ 将其应用于字符串向量就很简单了:
void center( std::ostream& os,
const std::vector< std::string >& strings,
int width ) {
for ( auto&& string : strings ) {
center( os, string, width );
}
}
无论您是想使用 std::string
还是 iomanip
操纵符,还是 std::setfill
要点都是一样的:不要在同一个函数中实现 "iteration and formating"。
我有一个简单的功能,可以将文本行打印到控制台,居中,空的 space 填充有“=”符号。当我 运行 我的程序在 Linux 上使用此功能时,我看到在控制台顶部正确显示的文本 window 随后是我程序的菜单提示,但是在 Windows 它不打印任何内容并直接跳到菜单提示。这两个程序都是使用 GNU gcc 和 -std=c++11.
在代码块中编译和 运行void _print_center(vector<string>& tocenter)
{
int center;
for ( int x; x<static_cast<int>(tocenter.size());x++ )
{
char sfill = '=';
string line = tocenter[x];
center = (68/2)-(tocenter[x].length()/2);
line.replace(0, 0, center, sfill);
cout << std::left << std::setfill(sfill);
cout << std::setw(68) << line << endl;
}
}
您的问题得到了答案(未初始化的变量)。我建议您理清并简化您的代码,这样这类问题就不会经常出现。例如:
创建一个以单个字符串为中心的函数。
void center( std::ostream& os, const std::string& text, int width ) {
if ( text.size() >= width ) {
// Nothing to center, just print the text.
os << text << std::endl;
} else {
// Total whitespace to pad.
auto to_pad = width - text.size();
// Pad half on the left
auto left_padding = to_pad / 2;
// And half on the right (account for uneven numbers)
auto right_padding = to_pad - left_padding;
// Print the concatenated strings. The string constructor will
// correctly handle a padding of zero (it will print zero `=`).
os << std::string( left_padding, '=' )
<< text
<< std::string( right_padding, '=' )
<< std::endl;
}
}
一旦您测试该函数对单个字符串运行良好,依靠 C++ 将其应用于字符串向量就很简单了:
void center( std::ostream& os,
const std::vector< std::string >& strings,
int width ) {
for ( auto&& string : strings ) {
center( os, string, width );
}
}
无论您是想使用 std::string
还是 iomanip
操纵符,还是 std::setfill
要点都是一样的:不要在同一个函数中实现 "iteration and formating"。