C++ 帮助添加未知函数。(C+;+;)

C++ 帮助添加未知函数。(C+;+;),c++,C++,我是编程新手,正在复习在线教程。我正在尝试修改它要求我创建的一个短程序。最终,我将学习以下内容,但此刻我对它感兴趣 这是一个关于将球从塔上落下的简单模拟程序(假设空气阻力和重力变化可以忽略不计),我想为它添加一个函数,输出球的高度,从0到用户指定的秒数 我会用什么做这个,怎么做?该计划的所有其他方面都已完成。目前存在: double t_height; //User specified height from which ball is dropped int seconds; //Us

我是编程新手,正在复习在线教程。我正在尝试修改它要求我创建的一个短程序。最终,我将学习以下内容,但此刻我对它感兴趣

这是一个关于将球从塔上落下的简单模拟程序(假设空气阻力和重力变化可以忽略不计),我想为它添加一个函数,输出球的高度,从0到用户指定的秒数

我会用什么做这个,怎么做?该计划的所有其他方面都已完成。目前存在:

double t_height; //User specified height from which ball is dropped
int seconds;     //User specified number of seconds
double distance; //Distance fallen after specified number of seconds
double b_height; //Height of ball after a given number of seconds
以上所有问题都解决了,就我使用的变量而言。谢谢你抽出时间

如果有帮助的话,下面是我已经拥有的25行左右的代码

#include "stdafx.h"
#include "constant.h"
#include <iostream>

int main()
{
    using namespace std;
    cout << "Put tower height meters \n";
    double t_height;
    cin >> t_height;

    cout << "Put number of seconds here \n";
    double seconds;
    cin >> seconds;

    double distance{ constant::g*seconds*seconds / 2 };

    double b_height{ t_height - distance };
    if (distance > t_height)
        b_height = 0;

     cout << "After " << seconds << " seconds the ball is at " << b_height << " meters above the ground. \n";

    return 0;
}
#包括“stdafx.h”
#包括“常数.h”
#包括
int main()
{
使用名称空间std;
cout>t_高度;
cout>秒;
双距离{常数::g*秒*秒/2};
双b_高度{t_高度-距离};
如果(距离>t_高度)
b_高度=0;

cout在浏览了另一个部分之后,我找到了“while”循环,并且成功地实现了它,而没有创建另一个函数

#include "stdafx.h"
#include "constant.h"
#include <iostream>

int main()
{
    using namespace std;
    cout << "Put tower height meters \n";
    double t_height;
    cin >> t_height;

    cout << "Put number of seconds here \n";
    int seconds;
    cin >> seconds;

    int count{ 0 };
    while (count <= seconds)
    {
        double distance{ constant::g*count*count / 2 }; //Modified from original to include int count as the new second, as at the end of the loop, count is increased by one for each run.

        double b_height{ t_height - distance };
        if (distance > t_height)
        {
            b_height = 0;
        }
        cout << "After " << count << " seconds the ball is at " << b_height << " meters above the ground. \n";

        count = count + 1;
    }

    return 0;
}
#包括“stdafx.h”
#包括“常数.h”
#包括
int main()
{
使用名称空间std;
cout>t_高度;
cout>秒;
整数计数{0};
while(计算t_高度)
{
b_高度=0;
}

cout修改代码的方法如下:

#include "stdafx.h"
#include "constant.h"
#include <iostream>

using namespace std;

void main()
{
    double       t_height;
    int          seconds;
    int          count = 0;

    cout << "Put tower height meters \n";
    cin  >> t_height;

    cout << "Put number of seconds here \n";
    cin  >> seconds;

    while (count <= seconds)
    {
        double distance = (constant::g * count * count) / 2; 
        double b_height = t_height - distance;

        if (distance > t_height){
            b_height = 0;
        }
        cout << "After " << count << " seconds the ball is at " << b_height << " meters above the ground. \n";

        count++;
    }
}
#包括“stdafx.h”
#包括“常数.h”
#包括
使用名称空间std;
void main()
{
双t_高度;
整数秒;
整数计数=0;
cout>t_高度;
cout>秒;
while(计算t_高度){
b_高度=0;
}

你能让它几乎正常工作吗?只要把计算b_高度的代码行移到一个函数上就行了。我很感激你的兴趣。它实际上工作得很好,但是上面的问题是关于添加一个我还不知道的特定函数。谢谢你的阅读。这可能会有帮助:哈哈,我为这个问题道歉缺乏清晰性。我熟悉如何创建和调用函数。(对不起,点击回车,给我一点时间编辑)续:问题的关键部分是“我想在其中添加一个函数,从0到用户指定的秒数,每秒输出球的高度。”目前,我遇到了一个“while”我以前没有见过的循环,现在正在实现一个循环。一旦我成功了,我会编辑这个问题并关闭它(如果可能)。执行第一步,即将计算高度的代码放入函数中。循环(持续或暂时)是下一步。谢谢!我已经复习了一些我还没有学过的东西,现在我知道了。