2016-09-20 55 views
0

所以我是新来的C++,并且我正在使用for循环制作表格。试图在C++中制作表格,遇到问题

我一直有麻烦的for循环有起始列,行,和calcWind值都在一个循环。所以我决定把它分成两部分。

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

我现在在Calcwind上遇到麻烦,在控制台屏幕上显示实际计算结果。再次

感谢提前的帮助:)

Here's what the console image should look like, I'm just laying down the values with my code for now.

#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; 
} 
+0

'std :: system(“pause”);'是非常危险的,只是你知道的。 – CoffeeandCode

+0

在第一次循环之后,您的变量'rows'已经等于'-30'。 – GAVD

+0

请详细说明你的_“麻烦”_。这很含糊。 –

回答

0

在这里,您创建rows

int rows = 40; 

你再继续减5,直到它的-30下(在第一环):

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

然后,在打印输出calcWind的循环中,rows仍等于-35。条件rows >= -30在第一次迭代中失败并且从不运行calcWind或打印结果。

0

你可以试试这个。

#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; 
}