为什么代码不断跳转以返回main();

Why Does Code Keep Jumping to return main();?

本文关键字:main 返回 代码 为什么      更新时间:2023-10-16

嘿,伙计们,我是c++新手,为了练习,我正在制作一款"制作你自己的冒险游戏"我不知道问题出在哪里。我相信这主要与我的char变量有关。我将发布我的main.cpp的代码。还有其他外部头文件吗?但我看不出有什么理由发布它们。我的代码也会运行而不给我错误。如果我的if-else语句/我的char变元被破坏,我不知道它怎么能做到这一点。

谢谢你的帮助。

#include <iostream>
//LVL1
#include "C:UsersQuestionMarkDesktopMake Your Own AdventureLVL1Dog.h"
#include "C:UsersQuestionMarkDesktopMake Your Own AdventureLVL1Dream.h"
#include "C:UsersQuestionMarkDesktopMake Your Own AdventureLVL1GTFO.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 nown";
        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 Coden";
    }
}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 numbern";
    int x;
    cin >> x;
    if(x == 0){
        Dog();
    }else if(x == 1){
         GTFO();
    }else{
         Dream();
   }
}else{
cout << "Invalid Answernnn";
return main();
}
return 0;
}

顺便说一句。在The Header Dog中,我调用了level2的所有函数我只是想知道为什么我的程序在没有我打电话的情况下运行得很好我的GTFO标题和Dream标题中的所有level2功能。

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

pps:还想知道为什么我不必调用main.cpp中的level2函数?

最后的想法:谢谢你抽出时间,祝你今天愉快!

ps最后的想法:这是门户1参考资料。

不允许在C++程序中调用main()。时期在C中,是,但在C++中,不是。当您在程序中调用main()时,您调用的是未定义的行为,程序可以执行任何操作。

您不会将c字符串与==进行比较。请改用strcmp()。由于这是c++,所以无论如何都应该使用std::string。此外,z[5]不够大,无法容纳"Dream"或其他5个字符串。