2016-12-04 56 views
0

所以我在遊戲的一個階段創建了一個菜單。選擇選項1或3後,你死了,你必須選擇另一個,直到你選擇選項2或4.之後,我的循環應該結束,讓玩家按任何按鈕,自動保存他的遊戲,關閉程序,並在函數int main()轉到遊戲的另一部分,但在按2或4後,循環仍然有效,我可以永久按下這4個按鈕。任何人都知道什麼是錯的?我的循環不想結束

 cout<<tr("1. Option")<<endl; 
    cout<<tr("2. Option")<<endl; 
    cout<<tr("3. Option")<<endl; 
    cout<<tr("4. Option")<<endl; 



    while((wyborstage[2]!='2')||(wyborstage[2]!='4')) 
    { 
     wyborstage[2]=getch(); 
     switch((wyborstage[2])) 
     { 
      case '1': 
      { 
       cout<<endl; 
       cout<<"Sample Text"<<endl<<endl; 
       cout<<"Try again"<<endl; 
      } 
      break; 

      case '2': 
      { 
       cout<<endl; 
       cout<<"Sample text"<endl; 

      } 
      break; 

      case '3': 
      { 
       cout<<endl; 
       cout<<"Sample text"<<endl; 
       cout<<tr("Try again")<<endl; 
      } 
      break; 

      case '4': 
      { 
       cout<<endl; 
       cout<<"Sample text"<<endl; 
       ; 
      } 
      break; 

      default: 
      { 
       cout<<endl; 
       cout<<"Wrong answer"<<endl; 
      } 

     } 
    } 

    //End of this part of game 

cout<<"Press any button to continue"; 
getch(); 

system("cls"); 

fstream save3; 
save3.open("save.conf", ios::out); 
save3<<3<<endl; 

integ--; 
+0

對於這種情況,您應該使用do ... while循環 –

+2

'((wyongstage [2]!='2')||(wyongstage [2]!='4'))'總是如此。我的意思是,如果'wyborstage [2] = 2'那麼它不是4.如果'wyongstage [2] = 4'那麼它不是2.如果'wyborstage [2] =任何其他值'那麼它不是2或4. – drescherjm

回答

1

你的循環永遠不會結束,因爲它總是如此,你可能想要一個& &,而不是||。您當前正在說循環,而值不是2或值不是4.將值設置爲2或4時,仍然會有||的另一側。這是真的,而這一段時間將繼續。

+0

好吧, 有用。非常感謝你 :) – hueh