我在设计一个简单的RPG只是为了让我开始使用C++编码,但是我的else语句无法正常工作,而且我感觉我的代码比本应该更笨重。你能不能很好的给我一些关于如何改进我的代码以及如何修正else语句的提示。我知道“系统”(“cls”)“对任何事情都很憎恨,但我把它放在那里,因为它使它看起来比其他方法更清洁。C++ Beginner else statement not working
我仍然在C++一个完整的新手的方式
#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
int save = 0;
int menuinput;
int raceinput;
string race;
int cont1;
int main(void)
{
int save = 0;
int menuinput;
int raceinput;
string race;
int cont1;
cout << "(Insert Generic RPG Name Here) \n";
if (save == 1){
cout << "Main Menu\n";
cout << "[1.] Continue\n";
cout << "[2.] New Game\n";
cout << "[3.] Options\n";
cin >> menuinput;
}
if (save == 0){
cout << "Main Menu\n";
cout << "[1.] New Game\n";
cout << "[2.] Options\n";
cin >> menuinput;
system("cls");
}
if (menuinput == 1)
{
cout << "Please select your race\n";
cout << "[1.] Human\n";
cout << "[2.] Elf\n";
cout << "[3.] Dwarf\n";
cout << "[4.] Orc\n";
cin >> raceinput;
if (raceinput == 1){
race = "Human";
cont1 = 1;
}
if (raceinput == 2){
race = "Elf";
cont1 = 1;
}
if (raceinput == 3){
race = "Dwarf";
cont1 = 1;
}
if (raceinput == 4){
race = "Orc";
cont1 = 1;
}
else {
system("cls");
cout << "Invalid Input, please try again.";
Sleep(1000);
main();
}
if (cont1 = 1){
system("cls");
cout << race;
cin >> race;
}
else
{
system("cls");
cout << "Invalid Input, please try again.";
Sleep(10000);
main();
}
}
}
编辑: 要重申的是,当我尝试使用的第一个else语句(在主菜单出现)的其他语句不会激活,并立即结束程序。 else语句消息不显示。而在第二个else语句中,else语句消息显示,但Sleep函数不起作用,它显示大约半毫秒然后消失。
“不工作”不是一个有用的问题描述。 –
@SamVarshavchik要补充,真正的问题是什么?具体是什么*不工作*根据需要? – Li357
你在'main()'中调用'main()'。在继续之前,请看看:http://stackoverflow.com/questions/2532912/call-main-itself-in-c。 –