将数组传递给布尔函数
Passing array to Boolean Function
我正在做一个项目,我已经完成了我所有的代码,我只是有一个问题(代码是在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.";
}
}
相关文章:
- 如何编写一个使用n倍三元条件语句的C++布尔函数
- 为什么我不能在返回 const 的布尔函数中为类成员变量赋值?C++
- 程序中的布尔函数返回输入的范围无论如何都是无效的
- 类中的虚拟布尔函数参数不起作用
- 我无法在 c++ 中调用布尔函数(已关闭)
- 如何使用布尔函数按降序对数组进行排序?
- 在布尔函数 c++ 中使用 elses
- 布尔函数在一段时间内不起作用
- 我正在尝试创建一个布尔函数,该函数计算字符并在字符为"()*-+"时返回 true
- 如何编写正确的"布尔"函数?
- 无法在 c++ 中" xor() "命名布尔函数
- 递归布尔函数的问题
- 布尔函数始终返回真正的 C++
- 具有单个参数的布尔函数映射
- 返回类型布尔函数中的声明语法错误
- 为什么我的布尔函数返回 true 会导致读取访问冲突?
- 单调布尔函数
- 布尔函数无法返回真或假
- 在C 中调用布尔函数
- 模板布尔函数没有<>,这是什么意思?