将数组传递给布尔函数

时间:2015-05-21 18:21:00

标签: c++ arrays function struct boolean

我正在为一个类的项目工作,我已经完成了我的所有代码,我只有一个问题(代码是C ++)。我不太擅长使用布尔函数,特别是在这个程序的情况下。如果你们可以帮我写这个代码或者让我朝着正确的方向努力,我会很感激。该程序应该是由结构组成的苏打机程序。我的问题是如何将数组传递给布尔函数,以检查是否有任何饮料留给客户想要购买的饮料。如果没有饮料,该程序将打印出“抱歉我们已售罄。请另外选择。”如果仍然有饮料,那么就什么都不做,继续该计划。它几乎可以验证剩余的饮料量。我试图写这个函数,但我不确定它是否正确,我会发布给你们看看它。如果你们需要任何其他信息,请告诉我。谢谢你们之前的帮助。

#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>

using namespace std; 

struct Machine
{
   string name;

   double cost;

   int drinks;
};

int displayMenu(int choice);

double inputValidation(double);

bool drinksRemaining(); // this is the function that will check if there are any drinks of the choice left 

int displayMenu(int choice)
{

   cout << "SOFT DRINK MACHINE\n";

   cout << "------------------\n";

   cout << "1) Cola ($0.65)\n";

   cout << "2) Root Beer ($0.70)\n";

   cout << "3) Lemon-Lime ($0.75)\n";

   cout << "4) Grape Soda ($0.85)\n";

   cout << "5) Water ($0.90)\n";

   cout << "6) Quit Program\n";

   cout << endl;

   cout << "Please make a selection: ";

   cin >> choice;

   return choice; 

}

double inputValidation(double amount)
{

   while (amount < 0.00 || amount > 1.00)
   {
      cout << "Please enter an amount between $0.00 and $1.00.\n";

      cout << "It should also be equal to or greater than the drink price : \n";

      cin >> amount;
   }

   return amount;

}

bool drinksRemaining() // this is the function that I am having trouble with
{
   if (drinks <= 0)
   {
      cout << "Sorry we are out of this drink. Please choose another one.";

      cin >> drinkChoice;

      return true;
   }

   else
   {
      return false; 
   }
}


int main()
{
   const int DRINK_NUMS = 5;

   int selection = 0;

   double amountInserted = 0.00;

   double changeReturned = 0.00;

   double profit = 0.00;

   Machine drink[DRINK_NUMS] = { { "Cola", .65, 20 }, { "Root Beer", .70, 20 }, { "Lemon-Lime", .75, 20 }, { "Grape Soda", .85, 20 }, { "Water", .90, 20 } };

   do
   {
      profit += amountInserted - changeReturned;

      selection = displayMenu(selection);

      if (selection == 1)
      {
         cout << "Please enter the amount you want to insert:\n";

         cin >> amountInserted;

         inputValidation(amountInserted);

         changeReturned = amountInserted - drink[0].cost;

         cout << setprecision(2) << fixed << "CHANGE : $" << changeReturned << endl;

         drink[0].drinks--;

      }

      else if (selection == 2)
      {
         cout << "Please enter the amount you want to insert:\n";

         cin >> amountInserted;

         inputValidation(amountInserted);

         changeReturned = amountInserted - drink[1].cost;

         cout << setprecision(2) << fixed << "CHANGE : $" << changeReturned << endl;

         drink[1].drinks--;

      }

      else if (selection == 3)
      {
         cout << "Please enter the amount you want to insert.\n";

         cin >> amountInserted;

         changeReturned = amountInserted - drink[2].cost;

         cout << setprecision(2) << fixed << "CHANGE : $" << changeReturned << endl;

         drink[2].drinks--;


      }

      else if (selection == 4)
      {
         cout << "Please enter the amount you want to insert.\n";

         cin >> amountInserted;

         changeReturned = amountInserted - drink[3].cost;

         cout << setprecision(2) << fixed << "CHANGE : $" << changeReturned << endl;

         drink[3].drinks--;

      }

      else if (selection == 5)
      {
         cout << "Please enter the amount you want to insert.\n";

         cin >> amountInserted;

         changeReturned = amountInserted - drink[4].cost;

         cout << setprecision(2) << fixed << "CHANGE : $" << changeReturned << endl;

         drink[4].drinks--;

      }

      else if (selection == 6)
      {
         cout << endl;

         cout << "SOFT DRINK MACHINE REPORT\n";

         cout << "--------------------------\n";

         cout << "Total amount earned: $" << profit << endl;

         cout << endl;
      }

      else
      {
         cout << "Invalid selection. Please try again.";

         displayMenu(selection);
      }

   }while (selection != 6);

   system("PAUSE");

   return 0; 
}

1 个答案:

答案 0 :(得分:2)

该函数需要一个对象或对Machine类型的对象的引用。

bool drinksRemaining(Machine const& m);

可以非常简单地实施:

bool drinksRemaining(Machine const& m)
{
   return (m.drinks > 0);
}

您可以将其用作:

  if (selection == 1)
  {
     if ( drinksRemaining(drink[0]) )
     {
        cout << "Please enter the amount you want to insert:\n";

        cin >> amountInserted;

        inputValidation(amountInserted);

        changeReturned = amountInserted - drink[0].cost;

        cout << setprecision(2) << fixed << "CHANGE : $" << changeReturned << endl;

        drink[0].drinks--;
     }
     else
     {
        cout << "Sorry we are out of this drink. Please choose another one.";
     }
  }