为什么代码不断跳转以返回main();
Why Does Code Keep Jumping to return main();?
嘿,伙计们,我是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个字符串。
相关文章:
- 为什么要从main()返回NULL?
- 使用 Main() 返回值作为 python 脚本的参数
- 为什么 c++ 中的 main() 函数不采用除 int 和 void 之外的任何其他返回类型
- 我的运算符重载没有在我的 int main 中返回?
- 外部函数在 main() 上调用时返回分段错误 11.(C++)
- 动态数组传递给 main 中用随机值填充的函数,返回值3221226356
- 内存浪费?如果main()应该只返回0或1,那么为什么main是用int而不是短int甚至char声明的
- 无法从Linux,C ++执行"my_script" python脚本"main(filename)"功能;pModule = PyImport_Import(pName);返回空值
- 如何使类函数返回数组,然后将其调用并存储在 main 中
- 程序流并以安全而雄辩的方式正确返回Main()
- (C )当在同一类中调用时,污点指针返回正确的值,而从MAIN调用时为0
- c++抛出0xC0000005:写入位置0xFEEEFEEE的访问冲突.从main返回时
- c++不让main返回
- main:返回0挂起,退出0关闭.如何调试
- 在OS X中从main返回时出现中止陷阱6,但在linux中没有
- 从main返回0会中断我的程序
- main()返回时终止线程
- 当程序关闭(不从main返回)时,windows会自动释放内存吗?
- 在C/ c++中通过int main()返回0,在任何操作系统上,清除程序在任何(RAM,缓存或任何…)内存中使用的所有
- 从对象内部向main返回值