c++ - C++:如何使我的计算程序的每个答案对齐?

标签 c++

这是我的代码:

#include<iostream> 
int main() {

    int width, length, sq_cm, sq_m{}, sq_in, sq_ft, cm, m, in, ft;
    std::cout << "Find the Area and Perimeter of a Rectangle \n\n";
    std::cout << "Input the length of the rectangle : \b";
    std::cin >> length;
    std::cout << "Find the width of the rectangle : \b";
    std::cin >> width;
    sq_cm = (length * width);
    sq_m = (sq_cm / 100);
    sq_in = (sq_m * 39.37);
    sq_ft = (sq_in / 12);
    cm = (length + width);
    m = (cm / 100);
    in = (m * 39.37);
    ft = (in / 12);
    std::cout << "Area :" << sq_cm << std::endl;
    std::cout << sq_m << std::endl;
    std::cout << sq_in << std::endl;
    std::cout << sq_ft << std::endl;
    std::cout << "Perimeter :" << cm << std::endl;
    std::cout << m << std::endl;
    std::cout << in << std::endl;
    std::cout << ft << std::endl;
    return 0;
这是我调试时的样子
Find the Area and Perimeter of a Rectangle

Input the length of the rectangle :87
Find the width of the rectangle :68
Area :5916
59
2322
193
Perimeter :155
1
39
3
这就是我想要的样子
Find the Area and Perimeter of a Rectangle

Input the length of the rectangle :87
Find the width of the rectangle :68
      area:5916 sq cm
           59   sq m
           2322 sq in
           193  sq ft

Perimeter: 155  cm
           1    m
           39   in
           3    ft

最佳答案

要设置文本格式,请尝试以下操作:
使用\ t“制表”输出。
就像Sugar在评论中伤心一样,最好使用"\n"而不是endl

#include<iostream> 
int main() {

    int width, length, sq_cm, sq_m{}, sq_in, sq_ft, cm, m, in, ft;
    std::cout << "Find the Area and Perimeter of a Rectangle \n\n";
    std::cout << "Input the length of the rectangle : \b";
    std::cin >> length;
    std::cout << "Find the width of the rectangle : \b";
    std::cin >> width;
    sq_cm = (length * width);
    sq_m = (sq_cm / 100);
    sq_in = (sq_m * 39.37);
    sq_ft = (sq_in / 12);
    cm = (length + width);
    m = (cm / 100);
    in = (m * 39.37);
    ft = (in / 12);
    std::cout << "\tArea :\n";
    std::cout << "\t" << sq_cm << "\n";
    std::cout << "\t" << sq_m << "\n";
    std::cout << "\t" << sq_in << "\n";
    std::cout << "\t" << sq_ft << "\n";
    std::cout << "\tPerimeter :\n";
    std::cout << "\t" << cm << "\n";
    std::cout << "\t" << m << "\n";
    std::cout << "\t" << in << "\n";
    std::cout << "\t" << ft << "\n";
    return 0;
}
它将是这样的:
Find the Area and Perimeter of a Rectangle

Input the length of the rectangle :87
Find the width of the rectangle :68
      Area:
      5916 
      59   
      2322 
      193  

      Perimeter: 
      155  
      1    
      39   
      3   

关于c++ - C++:如何使我的计算程序的每个答案对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64243264/

相关文章:

c++ - 如何链接重载的模板方法

c++ - C++20 中的位域初始化,构造为 `|| new int`

c++ - 奇数 cpp 读取访问冲突

c++ - 返回具有已删除移动/复制构造函数的临时类型

c++ - 使用单个数字表示多个变量

c++ - 如何在结构中动态添加数据,该结构是指向指针数组的指针

c++ - 有没有办法配置 clang-format 以将嵌套的命名空间声明保持在同一行?

c++ - std::unordered_multiset 插入的复杂性

c++ - 互斥锁优先级

C++ 预期主表达式在 ';' 标记之前