C++帮助 - 我似乎无法弄清楚此代码的最后一部分
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。
相关文章:
- 任何人都可以弄清楚这段代码如何显示运行错误?它打印无限时间 -1 以及正确答案
- 试图弄清楚我需要做哪些更改才能使代码正常工作.使用Visual Studio
- 弄清楚这段代码是怎么回事(递归下降案例研究)
- 我无法弄清楚我的代码未显示预期结果的问题
- 我无法弄清楚我的代码出了什么问题
- 堆排序,我无法弄清楚我的代码出了什么问题,输出顺序不正确
- 我需要帮助弄清楚如何将getline纳入PresentStringPrompt,以摆脱重复的代码
- Visual Studio 2019不活跃的代码没有弄清楚
- 无法弄清楚为什么这个Arduino代码不起作用?
- 我需要帮助弄清楚为什么此C 程序不会使用GNU CC编译器在代码块中构建和运行
- 无法弄清楚如何让G 接受我的代码
- 无法弄清楚为什么代码行会引发异常
- C++ 代码跳过代码,教授无法弄清楚
- 无法弄清楚我自己的代码
- 需要帮助弄清楚此 c++ 代码的计算方式
- C++代码崩溃,无法弄清楚原因
- 无法弄清楚为什么此代码在发送数据包数据时会导致 SIGSEGV
- 我正在阅读开源代码(C++),无法弄清楚为什么他们以这种方式放置枚举
- 我无法弄清楚为什么我的代码无法通过第 60 行的 cin 。帮助??C++
- 无法弄清楚为什么代码崩溃C++