2014-11-21 64 views
-2

錯誤是它應該返回領導者已採取的GOLD_PIECES數量的值。但是,無論我做什麼,它總是返回值0。這是一個C++初學者級別的代碼,所以任何擁有一些C++背景的人都可以幫助我解決這個問題。問題是在第41行(返回0以上兩行); cout < < < < <「保留在額外的」< <(GOLD_PIECES%survivors);C++遊戲開發代碼錯誤

// Lost Fortune 
// A personalized adventure 

#include <iostream> 
#include <string> 

using std::cout; 
using std::cin; 
using std::endl; 
using std::string; 

int main() 
{ 
    const int GOLD_PIECES = 900; 
    int adventurers, killed, survivors; 
    string leader; 

    //get the information 
    cout << "Welcome to Lost Fortune\n\n"; 
    cout << "Please enter the following for your personalized adventure\n"; 

    cout << "Enter a number: "; 
    cin >> adventurers; 

    cout << "Enter a number, smaller than the first: "; 
    cin >> killed; 

    survivors = adventurers - killed; 

    cout << "Enter your last name: "; 
    cin >> leader; 

    //tell the story 
    cout << "\nA brave group of " << adventurers << " set out on a quest "; 
    cout << "-- in search of the lost treasure of the Ancient Dwarves. "; 
    cout << "The group was led by that legendary rogue, " << leader << ".\n"; 

    cout << "\nAlong the way, a band of marauding ogres ambushed the party. "; 
    cout << "All fought bravely under the command of " << leader; 
    cout << ", and the ogres were defeated, but at a cost. "; 
    cout << "Of the adventurers, " << killed << " were vanquished, "; 
    cout << "leaving just " << survivors << " in the group.\n"; 

    cout << "\nThe party was about to give up all hope. "; 
    cout << "But while laying the deceased to rest, "; 
    cout << "they stumbled upon the buried fortune. "; 
    cout << "So the adventurers split " << GOLD_PIECES << " gold pieces."; 
    cout << leader << " held on to the extra " << (GOLD_PIECES % survivors); 
    cout << " pieces to keep things fair of course.\n"; 

    return 0; 
} 
+0

調試,調試,調試! – 2014-11-21 04:51:44

+0

請使用更具描述性的問題標題。 – 2014-11-21 04:52:19

+0

你在哪一行得到'0'? – Himanshu 2014-11-21 04:57:09

回答

1

啊我記得這本書。這正是啓發我在C++編程的偉大之路上的原因!無論如何,要回答你的問題。你確定你沒有傳遞一個可被900整除的變量嗎?請輸入前兩個數字,然後嘗試8和1,然後再次嘗試該程序。去年我參加這個項目時,我沒有遇到教科書中的代碼問題。快樂的編碼!

+0

謝謝!現在我可以繼續閱讀第二章。 – 2014-11-21 05:15:21