为什么代码保持跳转以返回main();?

时间:2013-11-28 03:54:37

标签: c++ if-statement char adventure

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

感谢您的帮助。

#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参考。

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:2)

您不能将c字符串与==进行比较。请改用strcmp()。因为这是c ++,所以你应该使用std :: string。此外,z [5]不足以容纳“Dream”或其他5个字符串。

相关问题