c++ - 如何在 2 个 int 函数之间重复抛出一个变量?

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

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

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

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

最佳答案

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

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

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

关于c++ - 如何在 2 个 int 函数之间重复抛出一个变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22949509/

相关文章:

c++ - Visual Studio C++ 中的 opencv 没有输出

c++ - C++ 类声明中的限定名称

c - C中整数的大小

c++ - 图像的边缘检测

c++ - VC11 中的 wchar 和标准输出

c++ - 为什么在使用复制分配运算符时需要删除资源?

c++ - 确定不安全转换的警告 clang

.net - 带有 WIX 的 C++ 可再发行包

string - 在 Swift 中将 Int 转换为 String

c - 通过套接字c读取整数