我的作业要求我在函数中使用布尔值。我需要将它们传递给功能吗?

时间:2019-04-15 20:06:37

标签: c++ function 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();
  }

我想发生的是,要传递的布尔INTIM是这样,所以我可以使用cout语句将其显示回main中。我知道结尾只会是1或0,但我只是想让它至少在显示结尾时显示出来。再次在该程序的函数中包含函数,这可能是我的问题,但我不这么认为。此后还有一些函数,这还不是程序的结尾,如果我需要发布整个内容,我会

2 个答案:

答案 0 :(得分:1)

以书面形式调用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,它也具有布尔值?

您不想遍历所有可能的布尔值,那会造成混乱,因此请考虑创建一个存储所有布尔值的数据结构。

这带来了一个更好的主意:将布尔值和函数封装在同一个数据结构中,这样您就不必传递任何信息了;都在同一个地方。

答案 1 :(得分:0)

  

是否需要将[布尔结果]传递给函数?

通常但并非总是如此,我更倾向于通过引用传递它们,是的,通过许多功能,它可以成为一个很大的链。叹。

但是您的问题是“您需要通过它们吗……”。

答案是否。

因为

a)您已将此帖子标记为C ++,并且

b)C ++的关键功能是用户定义的类。


  1. 考虑在类范围内声明故事的每个“冒险功能”。

  2. 每个“冒险功能”(作为类的一个属性)都是通过一个“隐藏”参数(指向类实例的“ this”指针)实现的。

  3. 因此..如果您将所有“结果”布尔值放置为该类的数据属性,则调用任何“冒险函数”也将“传递”所有类实例数据属性(您所有的笨蛋!)作为调用的一部分。实际上没有数据在移动,只有一个指针,即“ this”指针。


它可能看起来像这样:

#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:您提到使用布尔值返回结果。作为替代方案,您可能考虑使用stringstream ...带文本的单个流...像日志一样用于捕获正在进行的游戏,或向用户提供单个简单的总体报告。

祝你好运。