2016-09-20 6 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; 

당신은 다음 -30 아래가 될 때까지 5를 빼서 보관합니다 (에 첫 번째 루프) :

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