首页 > 解决方案 > 如何在5之前设置没有值的宽度

问题描述

如何使输出的整个第二行在输出中右对齐?我相信它设置为右对齐,但我觉得在输出五个之前需要一个变量来表示一个空格。如果有人可以尝试运行该程序以查看我正在谈论的输出显示,那么对于阅读此内容的人来说会更有意义。

    #include <iostream>
    #include <iomanip>
    #include <cmath>

    using namespace std;

    int main() { 

        cout<< "Temp | -------------- Wind Speed --------------" <<endl;

        for( int w=5; w<=45; w=w+5) {
            cout << setw(10) << right << w;

        }
        cout<< endl;

        for (int t=-5; t<=50; t=t+5) {
            cout << setw(5)<< right << t;
            for(int w=5; w<=45; w=w+5){
              cout << setw(10) << right << fixed<< setprecision(1)<< 35.74+(0.6215*t)-35.75*pow(w,0.16)+(0.4275*t*pow(w,0.16)); 
            }
            cout<<endl;
            }

        }

标签: c++

解决方案


#include <iostream>
    #include <iomanip>
    #include <cmath>

    using namespace std;

    int main() { 

        cout<< "Temp | -------------- Wind Speed --------------" <<endl;

        cout << "     ";

        for( int w=5; w<=45; w=w+5) {
            cout << setw(10) << right << w;

        }
        cout<< endl;

        for (int t=-5; t<=50; t=t+5) {
            cout << setw(5)<< right << t;
            for(int w=5; w<=45; w=w+5){
              cout << setw(10) << right << fixed<< setprecision(1)<< 35.74+(0.6215*t)-35.75*pow(w,0.16)+(0.4275*t*pow(w,0.16)); 
            }
            cout<<endl;
            }

        }

推荐阅读