2017-09-12 72 views
-5

我對編碼非常非常陌生。 以爲我會從C++開始,我已經寫了大約3天。 爲了教我自己,我一直在使用Sololearn,Hackerrank和編寫基於文本的冒險遊戲。在C++中發生奇怪的問題

這裏是我的問題。

我正在爲我的項目中的3個播放器編寫開始場景。 這些屬於單獨的if語句,其中的任何決定都是嵌套的if語句。 要包含字符健康狀況,所有這些都在一個while循環內,當Health> = 1時這是真的。

但是,我有一個奇怪的問題,其中選擇其中一個'選項'(嵌套的if語句在'Orc'下面),當運行時,它輸出多個選項,然後重複所有字符創建的文本,直到那個點,一次。

這隻發生在if語句是字符串時。 如果我更改代碼以使「Item」沒有字符串,並將「Bucket」,「Rug」和「Bars」設置爲int 1,2,3,則其工作方式與我所期望的相同。 這是C++中的字符串問題嗎?

我研究了所有我能想到的東西,包括使用getline代替cin獲取大量cin語句,以防造成問題,但沒有快樂。

請在下面找到我的代碼。我錯過了什麼嗎? 另外,這幾乎是全新的,有沒有更好的方法可以做到這一點?

最後,請注意,如果您覺得需要運行它才能更好地瞭解我遇到的問題,那麼只有'Orc'部分能夠正常工作 - 'Human'和'Elf'目前會創建無限循環因爲我沒有寫足夠多的內容來打破while循環。

在此先感謝您的幫助!

#include "stdafx.h" 
#include <iostream> 
#include <string> 

using namespace std; 


int main() 
{ 
//Name Entry 
string Name; 
cout << "Enter Name: " << endl; 
getline (cin, Name); 

//Age Entry 
int Age; 
cout << "Enter Age: " << endl; 
cin >> Age; 

//Race Entry 
string Race; 
cout << "Enter Race: " << endl; 
cin.ignore(); 
getline (cin, Race); 

//Stats 
int Strength; 
int Agility; 
int Speech; 
int Intellect; 
int Social; 
int Health=1; 

//Racial Starts 
while (Health >= 1) { 

    //Human 
    if (Race == "Human" || Race == "human") { 

     Strength = 10; 
     Agility = 10; 
     Speech = 8; 
     Intellect = 7; 
     Social = 20; 
     Health = 100; 

     cout << "Human Selected." << endl; 
     cout << "Starting Game!" << endl << endl; 
     cout << "You are in a finely furnished bedroom." << endl;} 

    //Elf 
    if (Race == "Elf" || Race == "elf") { 

     Strength = 8; 
     Agility = 14; 
     Speech = 12; 
     Intellect = 13; 
     Social = 10; 
     Health = 80; 

     cout << "Elf Selected." << endl; 
     cout << "Starting Game!" << endl << endl; 
     cout << "You are cleaning a finely furnished bedroom." <<endl;} 

    //Orc 
    if (Race == "Orc" || Race == "orc") { 

     Strength = 18; 
     Agility = 5; 
     Speech = 6; 
     Intellect = 3; 
     Social = 5; 
     Health = 200; 

     cout << "Orc Selected" << endl; 
     cout << "Starting Game!" << endl << endl; 
     cout << "You are locked in a cell." << endl << "The cell is made up of three stone walls." << endl << "There are no windows." << endl; 
     cout << "There is a rug on the cobbled stone floor, and a bucket in the corner." << endl << "Iron bars face into a guard's chamber." << endl << endl; 
     cout << "The guard is sleeping." << endl << "A shortsword rests in a scabbard on the table in fromt of him." << endl; 

     //Actions in this room 
     int Item; 
     cout << "Your escape lies with either the Bucket, the Rug or the Bars." << endl << "Pick an item." << endl; 

     string Item; 
     cin >> Item; 


     if (Item == "bucket" || Item == "Bucket") { 
      cout << "You place the Bucket on your head." << endl << "The bucket is full of your own excrement." << endl << "You die of disease." << endl; 
      Health = Health-1000; } 

     if (Item == "Rug" || Item == "rug") { 
      cout << "You lie on the rug and fall asleep." << endl << "The guard wakes up." << endl << "His irrational hatred for you causes him to draw his sword, enter your cell and sever your head." << endl; 
      Health = Health-1000; } 

     if (Item == "Bars" || Item == "bars") { 
      cout << "You try the bars." << endl << "You realise that this cell was not built to hold an Orc." << endl << "You bend the bars easily and step out of the cell." << endl; } 





    } 





} 

cout << "Game Over"; 

system("pause"); 
return 0; 
} 
+0

你在'while'循環中擁有所有的邏輯,所有的重複。使用調試器來瀏覽您的遊戲並檢查執行順序。 – xxbbcc

+4

「我一直在使用Sololearn,Hackerrank和編寫基於文本的冒險遊戲。」強烈考慮添加[一本好書](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)。 C++是一種非常複雜的語言,它有許多小小的黑人細節,會咬你,除非你知道正確的搜索關鍵字,否則不可能在線查找。 – user4581301

回答

1

所有的邏輯現在都在while循環中,所以它會一直重複。它會一直說你是一個獸人,並重新開始冒險。如果你想以更好的方式來開發這個,我會建議在視頻遊戲中查找「蜱蟲」和狀態機的概念,否則你會一直在觸及這個問題。或者,考慮將字符創建邏輯移至while循環之外,因爲您不希望這種邏輯繼續重複。

+0

另一個問題是,現在,您實際上並沒有檢查您的健康是否<= 1。while循環只會在循環結束時執行檢查*,這意味着您可以繼續玩整個負面健康的方式,它會奏效。確保你理解while循環邏輯以及何時/爲什麼要使用它,可能會有所幫助。 – OmegaNalphA