【问题标题】:How do I repeatedly throw a variable between 2 int functions?如何在 2 个 int 函数之间重复抛出一个变量?
【发布时间】:2014-04-08 22:34:49
【问题描述】:

我正在尝试创建一个执行以下操作的程序:
公交车每次停靠,都要装卸乘客。每个公交车站下车的乘客数量必须随机生成。每个站点的候车人数也是随机生成的。公共汽车应在不超过公共汽车容量的情况下装载尽可能多的等候乘客。

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
using namespace std;

void stop_bus(int); /* Stop the bus */ 
int unload_pass(int); /* Unload passengers */ 
int load_pass(int); /* Load passengers */ 
void start_bus(); /* Start the bus */


int main()
{
    srand(time(0));
    int stops = 7, currentStop = 1;
    int maxPass = 50;

    load_pass(maxPass);
    while(currentStop < stops)
    {
        currentStop++;
        stop_bus(currentStop); 
        unload_pass(maxPass); 
        load_pass(maxPass); 
        //start_bus(); 
    }


    system("pause");
    return 0;
}
void stop_bus(int currentStop)
{
    cout << "Currently at stop #" << currentStop << endl;
    cout << "Please disembark the bus in an orderly fashion." << endl;
    cout << endl;
}
int unload_pass(int maxPass)
{
    int passOff;
    int currentPass;
    currentPass = load_pass(maxPass);

    passOff = 1 + rand() % 50;
    while (currentPass - passOff > 0 )
    {
        cout << currentPass << ": That number is not in the range." << endl;
        passOff = 1 + rand() % 50;
        cout << endl;
    }

    cout << "People leaving the bus: " << passOff << endl;
    currentPass = currentPass - passOff;
    cout << "Current bus load is " << currentPass << endl;
    cout << endl;
    cout << currentPass << ": That number is not in the range" << endl;
    cout << endl;
    return currentPass;
}
int load_pass(int maxPass)
{
    int passOn;
    int currentPass;
    currentPass = unload_pass(maxPass);

    passOn = 1 + rand() % 50;
    while (currentPass + passOn > maxPass)
    {
        cout << currentPass << ": That number is not in the range" << endl;
        passOn = 1 + rand() % 50;
        cout << endl;
    }
    cout << "People boarding the bus: " << passOn << endl;
    currentPass = currentPass + passOn;
    cout << "Current bus load is "  << currentPass << endl;
    cout << endl;
    return currentPass;
}

我很肯定我做错了。当我尝试运行它时,会弹出另一个窗口告诉我破解代码。这究竟是什么意思,我该如何解决?

【问题讨论】:

  • "弹出另一个窗口告诉我破解密码。"你能发布确切的错误信息吗?

标签: c++ visual-c++ int void


【解决方案1】:

消息到底说了什么?如果“break”在这种情况下意味着“停止”,那么你可能有一个无限循环。通过检查您的代码,看起来 load_pass() 以调用 unload_pass() 开始,而 unload_pass() 又以再次调用 load_pass() 开始。

这本质上是一个无限递归,很可能会导致您看到的错误。

您是否看到打印语句的任何输出?那将是另一个很好的线索。

【讨论】:

  • [i.imgur.com/2593kBr.jpg](This) 弹出,不允许显示任何内容。
  • 这证实了 user832981 的猜测。 load_pass 做的第一件事就是打电话给unload_passunload_pass 做的第一件事就是打电话给load_pass。显然这是一个无限循环。您将不得不重新考虑这些功能应该执行的逻辑步骤。
  • 我还不确定如何更新 'unload_pass' 中的变量,在 'load_pass' 中调用它,并在那里更新它。你知道我怎么能做到这一点,或者更好的是它叫什么,所以我可以查一下?
  • 您需要将当前乘客人数存储在一个变量中,然后将该变量传递给函数或将变量设为全局变量,以便您可以从任何地方访问它。请注意,全局变量通常会使您的代码变得更糟,因此您可能不想这样做。另一种选择是创建一个总线类并将变量和函数放在该类中,但您需要为此学习面向对象的编程技术。
【解决方案2】:

看这里:

int unload_pass(int maxPass)
{
    int passOff;
    int currentPass;
    currentPass = load_pass(maxPass);

int load_pass(int maxPass)
{
    int passOn;
    int currentPass;
    currentPass = unload_pass(maxPass);

这两个函数相互调用,直到堆栈溢出。

要找出问题所在,在调试器中运行代码、观察调用堆栈、检查变量值等。

【讨论】:

  • 我怎样才能在它们之间使用这个变量?变量 currentPass 应该包含小于 50 的数字。在 unload_pass 中,它应该从 'currentPass' 中减去,而 load_pass 应该添加到 当前通行证
猜你喜欢
  • 2019-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多