如何证明我的光标与终端程序中的输出列对齐?

时间:2016-07-01 01:24:38

标签: c++ formatting

.city_form {
    width: 100%;
    height: 50px;
    padding: 2px 2px;
    text-align: center;
    display: inline-block;
    border-radius: 4px;
}

city_form[type=email] {
    padding-left: 10px;
    width: 30%;
    height: 92%;
    font-size: 100%;
    border-radius: 8px;
}

city_form[type=text] {
    padding-left: 10px;
    width: 30%;
    height: 92%;
    font-size: 100%;
    border-radius: 8px;
}
你好。所以我遇到的问题是当你输入一个radius(rad)变量的值时,当用户输入时,光标想要从左到右工作,导致两位数字比输出列长。

当程序运行并输入超过一位数的任何内容时,它看起来像这样:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{

const double PI = 3.14159;
double rad = 0;
double area = 0;
double vol = 0;
int areaPi = 0;
int volPi = 0;

cout << setprecision(5) << fixed;
cout << setw(38) << left << "Enter radius for the sphere: " << right);
cin  >> rad;

area = (4 * PI * (rad * rad));
vol = ((4.0/3.0) * PI * (rad * rad * rad));
areaPi = (4 * (rad *rad));
volPi = (4 * (rad * rad * rad));

cout << right << "Surface area of the sphere: " << setw(12) << area << " (" << areaPi << "\u03C0)";
cout << "\n";
cout << "The volume of the sphere: " << setw(14) << vol << " (" << volPi << "π/3)";
cout << "\n";

return 0;
}

我希望7与它下面的列对齐。我尝试将宽度设置为比之前更小的宽度。单个数字最终会向左移动一个空格,如下所示:

//Enter radius for the sphere:           17
//Surface area of the sphere:   3631.67804 (1156π)
//The volume of the sphere:    20579.50889 (19652π/3)

2 个答案:

答案 0 :(得分:0)

我会将输出存储到一组字符串中。然后,您可以根据需要检查和操作数据。或者,您可以在打印前计算出所需空间的偏移量

 // convert to string for digit count
std::string output_1 = std::to_string(x);
std::string output_2 = std::to_string(y);
int o_1_2_dist = output_1.size() - output_2.size(); // difference in digits
std::string padding_1, padding_2;
if (o_1_2_dist < 0)
    padding_1 = std::string(abs(o_1_2_dist), ' ');
else
    padding_2 = std::string(o_1_2_dist, ' ');
std::cout << padding_1 << output_1 << '\n' << padding_2 << output_2;

你想要调整输出字符串,这样它就不会计算你不关心的数字的额外位数。也许做output_1 = std::to_string(floor(x));或类似的东西,所以你不计算小数后面的数字

答案 1 :(得分:0)

这可以通过计算输入的长度来解决。我使用c ++ 11的to_string将结果值转换为字符串并找出它们的长度。我还没有尝试过便携式。它似乎在linux下使用gcc 6.1.1。但由于某种原因它不能用于输入,所以我也改变了那个部分,以便用户输入std::string然后转换为double

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{

const double PI = 3.14159;
double rad = 0;
double area = 0;
double vol = 0;
int areaPi = 0;
int volPi = 0;

int width_col1 = 40;

//cout.fill('.');
cout << setprecision(5) << fixed;
cout << left << setw(width_col1) << "Enter radius for the sphere: " << right;
std::string input;
cin  >> input;

rad = stod(input);

area = (4 * PI * (rad * rad));
vol = ((4.0/3.0) * PI * (rad * rad * rad));
areaPi = (4 * (rad *rad));
volPi = (4 * (rad * rad * rad));

int indent = width_col1 + input.length() + 1; 

cout << left << setw(indent - to_string(area).length()) << "Surface area of the sphere: " << area << " (" << areaPi << "\u03C0)" << std::endl;
cout << left << setw(indent - to_string(vol).length()) << "The volume of the sphere:   " << vol << " (" << volPi << "π/3)" << std::endl;

return 0;
}
  • 这个解决方案类似于C程序员用printf做的事情。

  • 我很想知道为什么这不适用于输入。