【问题标题】:C++ game returns 0 automatically [closed]C ++游戏自动返回0 [关闭]
【发布时间】:2016-04-05 19:30:03
【问题描述】:

每当我尝试运行此游戏时,它都会自动返回 0。 该游戏是我在 Code::Blocks 上编写的基于文本的生存游戏。编译器是 MinGW。我是一个半知识的程序员。编译时没有错误。

// This game automatically returns 0 and ends for some reason...
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace std;

int hunger;
int warmth;
int thirst;
int choice;
// Declares variables for hunger, warmth, thirst, and the users choice

int start()
{
    cout<< "You are stuck in a forest, all alone"<< endl;
    cout<< "You must maintain your hunger, thirst, and warmth." << endl;
    int mainPage();
}

int hunt()
{
    srand(time(0));
    cout<< "You have chosen hunt!"<< endl;
    if ((rand() % 2) == 2){
        cout<< "You caught a deer!"<< endl;
        hunger = hunger + 1;
    }
    else{
        cout<< "You could not find anything..."<< endl;
    }
    int mainPage();

}
// The previous function is used for the hunting choice

int wood()
{
    cout<< "You have chosen find firewood!"<< endl;
    if ((rand() % 2) == 2){
        cout<< "You found firewood!"<<endl;
        warmth = warmth + 1;
    }
    else{
        cout<< "You could not find any firewood"<< endl;
    }
    int mainPage();
}
// Wood choice

int water()
{
    cout<< "You have chosen find water!"<< endl;

    if ((rand() % 2) == 2){
        cout<< "You have found a river!"<< endl;
        thirst = thirst + 1;

    }
    else{
        cout<< "You could not find anything..."<< endl;
    }
    int mainPage();
}
// Water choice

int mainPage()
{
    warmth = warmth - 1;
    hunger = hunger - 1;
    thirst = thirst - 1;
    // Subtracts one from each variable per turn
    if (hunger == 0){
        cout<< "You starved!"<< endl;
        cout<< "Game over!"<< endl;
        return 0;
    }
    if (thirst == 0){
        cout<< "You became dehydrated!"<< endl;
        cout<< "Game over!"<< endl;
        return 0;
    }
    if (warmth == 0){
        cout<< "You froze!"<< endl;
        cout<< "Game over!"<< endl;
        return 0;
    }
    // You die if any of the variables reach zero

    cout<< "Your hunger is"<< hunger<<  endl;
    cout<< "Your warmth is"<< warmth<<  endl;
    cout<< "Your thirst is"<< thirst<<  endl;
    cout<< "What would you like to do?"<< endl;
    cout<< "1 = hunt, 2 = find firewood, 3 = find water"<< endl;
    cin>> choice;
    if (choice = 1){
        int hunt();
    }
    if (choice = 2){
        int wood();
    }
    if (choice = 3){
        int water();
    }
    // The main page that takes the users choice as input and also tells you the amount of each variable

}
int main()
{
    hunger = 5;
    thirst = 5;
    warmth = 5;
    int start();
}
// the main function

【问题讨论】:

  • “这个网站说我的问题主要是代码,所以我会复制并粘贴这个” 考虑到您收到此消息是有原因的。使用无意义的填充文本是不合适的!
  • 投下我的反对票。请在下次发布问题之前阅读How to ask
  • 对不起,我不应该那样做。
  • 因此,下一步是您将问题具体化,以提供一个minimal reproducible example,准确描述您在调试代码时的问题和观察结果。
  • 我不知道代码的哪一部分是问题,那是我的问题。如果我在这个评论中有点粗鲁,我很抱歉,但我根本不知道。

标签: c++ compiler-errors


【解决方案1】:

您的代码有几个问题。首先,您没有正确调用您的函数。当你有

int start();

main() 中,它不调用start 函数,而是声明了一个名为start 的函数,该函数返回int,并且什么都不带。知道你的主要功能是必不可少的

int main() {}

因为你除了设置一些永远不会使用的变量之外什么都不做。一个打开了警告的好的编译器至少应该告诉你你有未使用的变量。

在声明函数而不是调用函数时调用函数的其他任何地方都会遇到同样的问题。

修复函数调用后会出现第二个问题。您将在定义函数之前使用它们。解决此问题的一种简单方法是在定义/使用任何函数之前声明所有函数的函数原型,以便编译器知道该函数将存在。

第三个问题是你使用

srand(time(0));

在函数hunt() 中。这意味着每次您致电hunt 时,您都会重新播种rand。而不是这样做,您可以将 srand(time(0)); 放在 main 中,然后每次执行程序时您只会为 rand 播种一次。

我看到的最后一件事是,声明返回int 的函数实际上都没有返回任何内容。如果你声明一个函数有一个返回值,那么你需要从函数中返回一些东西。如果您不想返回任何内容,则可以将返回类型设为void,这意味着该函数不返回任何内容。

【讨论】:

  • 我现在已经解决了第三个问题,但其他两个问题对我来说是如何解决的谜
  • @TylerBacon 我建议你阅读如何声明、定义和使用函数:tutorialspoint.com/cplusplus/cpp_functions.htm
  • @TylerBacon 要调用start 函数,您使用start();,而不是int start();
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-08
相关资源
最近更新 更多