错误是它应该返回领导者已经采取的GOLD_PIECES数量的值。但是,无论我做什么,它总是返回值0。这是一个C ++初学者级代码,所以具有一些C ++背景的人都可以帮我解决这个问题。问题出在第41行(返回0上方两行;)cout<<领导者<< “坚持额外的”<< (GOLD_PIECES%幸存者);
// 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 :(得分:1)
啊,我记得这本书。这是我开始进入C ++编程之路的原因!无论如何,回答你的问题。你确定你没有传递900可被整除的变量吗?请输入前两个数字,然后尝试8和1,然后再次尝试该程序。当我去年完成这个程序时,我对教科书中的代码没有遇到任何问题。快乐的编码!