[英]C++ Help - I can't seem to figure out the last part of this code
我正在嘗試讓該程序顯示孩子的當前身高和孩子的估計身高。
我讓它顯示第一個孩子的前后,但是無法顯示其余孩子的估計身高。
如果有人可以幫助我解決我,我將不勝感激。 謝謝!
這是我的代碼:
#include <iostream>
using namespace std;
int main()
{
double height [10];
double chgHeight [10];
for (int x = 0; x < 10; x = x + 1)
{
height[x] = 0.0;
chgHeight[x] = 0.0;
}
cout << "You will be asked to enter the heights of ten children."<< endl;
for (int x = 0; x < 10; x = x + 1)
{
cout << "Enter height of child " << endl;
cin >> height[x];
}
chgHeight[0] = height[0] * .05 + height[0];
for (int x = 0; x < 10; x = x + 1)
{
cout << "Child " << x+1 << ": Current " << height[x] << " Expected "<< chgHeight[x] << endl;
}
system("pause");
return 0;
}
chgHeight[0] = height[0] * .05 + height[0];
您只需設置第一個孩子的chgHeight
。
編輯:
對於您的輸出,您正在遍歷數組或高度,該數組或高度由子編號( x
)索引:
for (int x = 0; x < 10; x = x + 1)
{
cout << "Child " << x+1 << ": Current " << height[x]
<< " Expected "<< chgHeight[x] << endl;
}
您估計的身高是根據該循環中孩子的當前身高( height[x]
)計算得出的。 因此,您在那里就可以輸出估計的高度。
如果您不需要保存計算供以后使用,則實際上無需在代碼中創建第二個chgHeight[]
數組; 只需計算並輸出該循環中每個孩子的估計身高即可。
您沒有設置其余孩子的估計身高,只有第一個:
chgHeight[0] = height[0] * .05 + height[0];
將其循環。
chgHeight[0] = height[0] * .05 + height[0];
該行僅計算第一個孩子的估計身高。 您還需要將其放入循環中(將索引更改為循環變量)以計算所有10。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.