2016-01-16 44 views
1

我對C++和一般編碼相當陌生。我正在嘗試爲練習做一個基本的小型多選型遊戲,但我遇到了一個難題。需要幫助輸出來自不同功能的東西(C++)

該程序不輸出我想要的東西。下面是代碼:

#include <iostream> 
#include <cstdlib> 
#include <ctime> 
#include <string> 

using namespace std; 

void sword(int damage); 
void fists(int damage); 

static int enemyHealth = 250; 

int main() { 
    srand(time(0)); 

    string SOF; //Abreveation for "Sword Or Fists" 

    cout << "You will be fighting a mean bad guy. Are you using a sword, or your fists?\n"; 

    while (SOF != "sword" && SOF != "fists"){ 
     cout << "Please enter your choice of either 'sword' or 'fists': "; 
     cin >> SOF; 
    } 

    cout << "Okay! Time to fight! \n"; 

    if (SOF == "fists") { 
     void fists(); 
    } 
    else if (SOF == "sword") { 
     void sword(); 
    } 
    else{ (NULL); } 

    cout << "Congratulations! You have vanquished that foul beast!\n"; 

    system("pause"); 
} 

//This is for when the user chooses 'sword' 
void sword(int damage = rand() % 100 + 50) { 
    while (enemyHealth > 0){ 
     cout << "You deal " << damage << " damage with your sharp sword. \n"; 
     enemyHealth -= damage; 
    } 
} 

//This is for when the user chooses 'fists' 
void fists(int damage = rand() % 10 + 4) { 
    while (enemyHealth > 0){ 
     cout << "You deal " << damage << " damage with your womanly fists. \n"; 
     enemyHealth -= damage; 
    } 
} 

第一部分工作正常,但是當我進入我的選擇,要麼"fists""sword"的輸出是:

Okay! Time to fight! 
Congratulations! You have vanquished that foul beast! 

但我希望它輸出的傷害正在做用拳頭或劍。

如果我能得到一些幫助,這將是驚人的。謝謝!

+0

非常感謝你的回覆,但現在我已經糾正了錯誤,我得到了一個不同的錯誤:錯誤C2660:'拳':函數不接受0參數我收到'劍'相同的錯誤爲好。我對C++的一些小知識表示歉意,感謝您的幫助。 – buddymcdoo

回答

3

void fists();是聲明,沒有一個電話,改變fists();sword();

其他的事情來看待:

  • 默認參數聲明函數聲明main之前(或剛搬到那裏全功能在c)
  • 默認參數++進行評估一次,因此所有的「命中」會在你的代碼
  • 局部變量名是相同的通常不用大寫字母命名,SOF看起來loke它是一個#define d常數或這樣的。
+0

很好的答案。我還建議在其他{(NULL); } – T33C

+0

非常感謝你的迴應,但現在我已經糾正了錯誤,我得到了一個不同的錯誤:error C2660:'fists':函數不接受0個參數 我也收到了'sword'的錯誤。我對C++的一些小知識表示歉意,感謝您的幫助。 – buddymcdoo

+0

@buddymcdoo這是因爲默認參數應該在'main'上面的聲明中(也可以參考其他答案) – Vasfed

1

要調用的函數,不寫void fists();,只是

fists(); 

(你有什麼是一個聲明,在這裏沒有任何有用的效果,而不是打電話。)