0

嘿伙计们,我是 C++ 新手,为了练习我正在制作一个“制作自己的冒险游戏”我不知道问题是什么我相信它主要与我的 char 变量有关我将发布代码我的 main.cpp 怎么会有其他外部头文件但我认为没有理由发布它们我的代码也将运行而不会给我一个错误,如果我的 if else 语句被破坏/我的字符变量。

谢谢您的帮助。

#include <iostream>
//LVL1
#include "C:\Users\QuestionMark\Desktop\Make Your Own Adventure\LVL1\Dog.h"
#include "C:\Users\QuestionMark\Desktop\Make Your Own Adventure\LVL1\Dream.h"
#include "C:\Users\QuestionMark\Desktop\Make Your Own Adventure\LVL1\GTFO.h"

using namespace std;

int main(){

    cout << "Welcome to my 'MAKE YOUR OWN ADVENTURE GAME!!!'\n";
    cout << "Have Fun and enjoy the ride!\n";
    cout << "Would you like to put in a cheat code??\n";
    cout << "Yes or No, Cap Sensitive!\n";
        char y[3];
        cin >> y;
if(y == "Yes"){
        cout << "Please Enter Cheat Code now\n";
        char z[5];

    if(z == "Dog"){
        Dog();
    }else if(z == "Dream"){
        Dream();
    }else if(z == "GTFO"){
        GTFO();
    }else if(z == "Path"){
        Path();
    }else if(z == "Sword"){
        Sword();
    }else if(z == "Weird"){
        Weird();
   }else{
    cout << "Invalid Cheat Code\n";
    }
}else if(y == "No"){

    cout << endl;
    cout << "You wake up and your house is on fire what do you do ??\n";
    cout << "Quick Grab The Dog = 0, GTFO = 1, Go back to sleep = any other number\n";
    int x;
    cin >> x;
    if(x == 0){
        Dog();
    }else if(x == 1){
         GTFO();
    }else{
         Dream();
   }

}else{
cout << "Invalid Answer\n\n\n";
return main();
}
return 0;
}

在旁注中。在 The Header Dog 中,我调用了 level2 的所有函数,我只是想知道为什么我的程序运行良好,而无需我调用 GTFO 标题和 Dream 标题中的所有 level2 函数。

ps: 只是为了消除一些混淆 Path();,Sword();, 和 Weird(); 都是level2函数。

pps:也只是想知道为什么我不必在 main.cpp 中调用 level2 函数?

最后的想法:感谢您的时间,祝您有美好的一天!

ps 最后的想法:这是一个传送门 1 参考。

4

2 回答 2

2

You don't compare c strings with ==. Use strcmp() instead. Since this is c++, you should be using std::string anyway. Also, z[5] is not big enough to hold "Dream" or other 5 character strings.

于 2013-11-28T04:05:32.107 回答
2

不允许调用main()C++ 程序。时期。在 C 中,是的,但在 C++ 中,不是。当您main()在程序中调用时,您正在调用未定义的行为,并且程序可能会做任何事情。

于 2013-11-28T03:56:35.940 回答