对于我的家庭作业,我应该制作一个创造你自己的冒险故事。文本中的某些单词在所有大写字母中表示布尔值,如果玩家获得它们,我需要在结尾处显示,如状态效果或其他东西。我无法弄清楚如何将布尔值传递给函数,以便它可以在程序结束时显示它。我的程序在函数中有函数。
我已经尝试将boolean设置为true的函数设置为布尔本身,然后返回布尔值,但这只是结束它看起来的程序。我也试过通过第一个函数调用来查看它是否到达第二个但它似乎不想它。
void A1();
bool A100(bool INTIM);
void A167();
void A232();
void A290();
void A13();
void A212();
void A173();
void A159();
void A161();
int main() {
bool INTIM;
A1();
cout << INTIM << endl;
return 0;
}
void A1()
{
int choice;
cout << "Well, Mr Artanon, ...\n 1. ’It’s you who’ll get a rare cut
across that corpulent neck of yours if you don’t speed things along, you
feckless blob of festering lard.’\n 2. ’Surely in such an industrious
kitchen, there must be a starter or two ready to send along and sate His
Abhorentness’s appetite?’\n (enter a menu option): ";
cin >> choice;
while (choice != 1 && choice != 2)
{
cout << "Enter in a valid choice (1 or 2)";
cin >> choice;
}
if (choice == 1)
{
A100();
}
if (choice == 2)
{
A167();
}
}
bool A100(bool INTIM)
{
int choice;
INTIM = true;
cout << " Repugnis turns a paler...\n 1. Onwards, Mr Artanon.\n (enter
in a menu option): ";
cin >> choice;
while (choice != 1)
{
cout << "Enter in a valid option (1)";
}
return INTIM;
A232();
}
我想要发生的是,bool INTIM被传递,所以我可以用cout语句将它显示回main。我知道它最后会只是1或0,但我只是试图让它在显示时至少显示出来。这个程序中的函数内部还有函数,这可能是我的问题,但我不这么认为。在此之后还有功能,这不是程序的结束,如果我需要发布整个事情,我会
如上所述调用A100
,您需要传入INTIM
并接受返回值
INTIM = A100(INTIM);
但是...... INTIM
的初始状态从未使用过,所以你可以
INTIM = A100();
并改变A100
看起来更像
bool A100()
{
int choice;
cout << " Repugnis turns a paler...\n 1. Onwards, Mr Artanon.\n (enter in a menu option): ";
cin >> choice;
while (choice != 1)
{
cout << "Enter in a valid option (1)";
cin >> choice; // added here because otherwise choice never changes
// and this loop will go on for a long, long time.
}
A232(); // moved ahead of return. Code after a return is not run
return true;
}
但是因为A232
被调用并且可能设置额外的标志你无法返回,你有一个设计缺陷:如果A232
也修改了布尔值怎么办?你只能从一个函数返回一个东西。你可以通过引用传递A232
的布尔值,但是A232
然后调用B484
它还有一个布尔值?
你不希望传递每一个可能的布尔值,这将是一个令人困惑的混乱,所以考虑建立一个存储所有布尔值的数据结构来传递。
这导致了一个更好的想法:将布尔值和函数封装在同一个数据结构中,这样你就不必传递任何东西;它们都在同一个地方。
我需要将它们[布尔结果]传递给函数吗?
通常,但并非总是如此,我倾向于通过引用传递它们,是的,它可以通过许多功能成为一个大链。叹。
但你的问题是“你需要通过它们......”。
答案是不。
因为
a)你已将这篇文章标记为C ++,并且
b)C ++的关键特性是用户定义的类。
它可能看起来像这样:
#include <iostream>
using std::cout, std::cerr, std::flush, std::endl;
// using std::cin;
#include <iomanip>
using std::setw, std::setfill;
#include <sstream>
using std::stringstream;
#include <string>
using std::string;
namespace AS // Adventure Story
{
class CreateYourOwnAdventureStory_t
{
private:
// diagnostic purposes
stringstream ssUI;
// command line arguments concatenated into one string
// contents: strings convertable to ints to mimic cin
bool INTIM;
// other results go here
public:
int operator()(int argc, char* argv[]) {return exec(argc, argv);}
private:
int exec(int argc, char* argv[])
{
int retVal = 0;
// capture all command line arguments into a string
for (int i=1; i<argc; ++i)
ssUI << argv[i] << " ";
cout << "\n ssUI: " << ssUI.str() << "\n\n\n";
A1();
cout << "\n INTIM : " << INTIM << endl;
// ?more here?
return retVal;
}
void A1()
{
int choice = 0;
cout << "Well, Mr Artanon, ...\n "
"\n 1. ’It’s you who’ll get a rare cut across that corpulent neck of yours "
"if you don’t speed things along, you feckless blob of festering lard. "
"\n 2. ’Surely in such an industrious kitchen, there must be a starter or two "
"ready to send along and sate His Abhorentness’s appetite?’"
"\n (enter a menu option): ";
ssUI >> choice; // cin >> choice;
if (choice == 1) { A100(); }
if (choice == 2) { A167(); }
}
void A100()
{
int choice = 0;
INTIM = true;
ssUI >> choice; // cin >> choice;
cout << "\n\n A100() choice:" << choice
<< " INTIM: " << INTIM << endl;
}
void A167()
{
int choice = 0;
INTIM = false;
ssUI >> choice; // cin >> choice;
cout << "\n\n A167() choice:" << choice
<< " INTIM: " << INTIM << endl;
}
// other action-functions go here
}; // class CreateYourOwnAdventureStory_t
typedef CreateYourOwnAdventureStory_t CreateYOAS_t;
} // namespace AS
int main(int argc, char* argv[]){return AS::CreateYOAS_t()(argc,argv);}
笔记:
此示例获取命令行参数并将它们附加到字符串流。结果可以像你的cin语句一样使用。
您是否注意到您(可能)不需要您的功能的前向声明?编译器必须扫描很多类声明来决定各种问题,因此可以发现A100(和A167)实际上是在AS :: CreateYOAS_t ::的范围内。这些函数仍然可以移动到cpp文件中,因此您仍然可以利用单独的编译。 (并且可能会节省一些编译较小文件的工作量,并且只能保存更改的文件。)
您是否注意到访问INTIM的函数只使用bool,而不需要任何'this->'去引用?
Main调用一个简单的Functor。没有其他的。 Main调用operator()。简单,极简。 ctor和dtor目前是默认的。如果你需要使用ctor初始化结果或其他中间信息,我只需将它添加到operator()实现附近。
PS:您提到使用bools返回结果。作为替代方案,您可以考虑使用字符串流...带有文本的单个流...使用类似日志来捕获正在进行的游戏,或者使用单个简单的整体报告给用户。
祝好运。