【问题标题】:Trying to make a table in c++, having trouble尝试用 C++ 制作表格,遇到麻烦
【发布时间】:2016-09-20 04:26:41
【问题描述】:

所以我是 C++ 新手,我正在使用 for 循环制作表格。

我一直无法让 for 循环将起始列、行和 calcWind 值全部放在一个循环中。所以我决定把它分成两部分。

第一个 for 循环,放置行的所有起始值。下一个 for 循环放置列值,然后将行 # 和列 # 插入我为计算风速而创建的函数中。

我现在无法让Calcwind 在控制台屏幕上显示实际计算。

再次感谢您提前提供帮助:)

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

using namespace std;

double calcWind(double temperature, double windSpeed)
{
    double wind = 0;
    wind = 35.74 + (.621 * temperature) - (35.75 * pow(windSpeed, 0.16)) + (.4275 * temperature * pow(windSpeed, .16));
    wind = nearbyint(wind);
    return wind;
}

int main()
{
    int rows = 40;
    int columns = 5;

    for (rows; rows >= -30; rows = rows - 5)
    {
        cout << setw(6) << rows;
    }

    for (columns; columns <= 60; columns = columns + 5)
    {
        cout << endl << columns;
        for (rows; rows >= -30; rows = rows - 5)
        {
            cout << setw(6) << calcWind(rows, columns);
        }
    }


    system("pause");
    return 0;
}

【问题讨论】:

  • std::system("pause"); 非常危险,你知道吗。
  • 在第一个循环之后,您的变量rows 已经等于-30
  • 请详细说明您的“问题”。这很模糊。

标签: c++ iostream


【解决方案1】:

你在这里创建rows:

int rows = 40;

然后继续减去 5,直到小于 -30(在第一个循环中):

for (rows; rows >= -30; rows = rows - 5)

然后,在您打印calcWind 的输出的循环中,rows 仍然等于-35。条件 rows &gt;= -30 在第一次迭代时失败,并且永远不会运行 calcWind 或打印结果。

【讨论】:

    【解决方案2】:

    你可以试试这个。

    #include <iostream>
    #include <iomanip>
    #include <cmath>
    #include <math.h>
    #include <stdio.h>
    
    using namespace std;
    
    double calcWind(double temperature, double windSpeed)
    {
        double wind = 0;
        wind = 35.74 + (.621 * temperature) - (35.75 * pow(windSpeed, 0.16)) + (.4275 * temperature * pow(windSpeed, .16));
        wind = nearbyint(wind);
        return wind;
    }
    
    int main()
    {
    //    int rows = 40;
    //    int columns = 5;
    
        for (int rows = 40; rows >= -30; rows = rows - 5)
        {
            cout << setw(6) << rows;
        }
    
        for (int columns = 5; columns <= 60; columns = columns + 5)
        {
            cout << endl << columns;
            for (int rows = 40; rows >= -30; rows = rows - 5)
            {
                cout << setw(6) << calcWind(rows, columns);
            }
        }
    
        std::cout << "\nPress any key to continue. . .\n";
        cin.get();
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2020-11-02
      • 1970-01-01
      • 1970-01-01
      • 2020-06-15
      • 1970-01-01
      • 2018-06-06
      • 2012-08-18
      • 2012-03-19
      • 1970-01-01
      相关资源
      最近更新 更多