将数组传递给布尔函数

Passing array to Boolean Function

本文关键字:布尔 函数 数组      更新时间:2023-10-16

我正在做一个项目,我已经完成了我所有的代码,我只是有一个问题(代码是在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 MACHINEn";
   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 Programn";
   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 REPORTn";
         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; 
}

该函数需要一个对象或对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.";
     }
  }