2016-06-11 115 views
-2

我在設計一個簡單的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函數不起作用,它顯示大約半毫秒然後消失。

+1

「不工作」不是一個有用的問題描述。 –

+0

@SamVarshavchik要補充,真正的問題是什麼?具體是什麼*不工作*根據需要? – Li357

+1

你在'main()'中調用'main()'。在繼續之前,請看看:http://stackoverflow.com/questions/2532912/call-main-itself-in-c。 –

回答

1

這條線:if (cont1 = 1){

你有=,你應該有==

所以,你還從未發生過,因爲你沒有檢查如果cont等於1,你使它相等。

+0

感謝您的提示 – Max