如何正确地传递这些值
How do I pass these values correctly?
本文关键字:正确地 更新时间:2023-10-16
我应该使用一个结构来存储机器中的饮料名称、饮料成本和饮料数量。我应该创建一个包含五个结构体的数组,其中的元素初始化为名称、成本和数字。程序应该显示一个饮料列表,用户应该在第一个函数中做出选择(1-6)。对选择进行验证并按值传递回主例程。在第二个功能中,用户插入钱,找零的金额就会显示出来,从机器中该饮料的数量中减去1,然后循环。当用户退出时,它会显示机器赚取的总金额。我的问题是将数组传递给函数。我以为我做得对,但我得到错误到处与函数和数组。有人知道我如何传递数组并返回函数的值吗?由于
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
struct Drink
{
string drinkName;
double cost;
int numberInMachine;
};
struct Drink options[] = {{"Cola", .75, 0}, {"Root Beer", .75, 2},
{"Lemon-Lime", .75, 10},
{"Grape Soda", .80, 3}, {"Cream Soda", .80, 20}};
int getChoice(Drink, int);
void showTransaction(Drink&);
int main()
{
const int NUM_DRINKS = 5; // Number of drink options
Drink options[NUM_DRINKS];
getChoice(Drink, value);
showTransaction(choice);
system("pause");
return 0;
}
int getChoice(Drink, choice)
{
int choice;
cout << "Enter the number(1-6) of the drink you would like: " << endl;
cout << "Drink Name Cost Number in Machine " << endl;
cout << "1. Cola .75 " << endl;
cout << "2. Root Beer .75 " << endl;
cout << "3. Lemon-lime .75 " << endl;
cout << "4. Grape Soda .80 " << endl;
cout << "5. Cream Soda .80 " << endl;
cout << "6. Quit " << endl;
cout << " Enter the number of your selection: ";
cin >> choice;
while(choice != 1 && choice != 2 && choice != 3 && choice != 4
&& choice != 5 && choice != 6)
{
cout << "Please enter a valid number 1-6" << endl;
cin >> choice;
}
return choice;
}
void showTransaction(choice)
{
double moneyIn;
double moneyOut;
if(choice ==1)
{
cout << "Enter money inserted up to $1.00: ";
cin >> moneyIn;
while(moneyIn < options[0].cost)
{
cout << "Enter correct amount" << endl;
cin >> moneyIn;
}
if(moneyIn > options[0].cost)
{
cout << "Your change is: " << (moneyIn - options[0].cost) << endl;
}
}
}
我已经修复了一些东西在你的代码。我已经评论了大部分;请通读一遍,试着理解为什么我有我所做的。
需要注意的主要事项是:
- 你全局声明了一个名为
options
的数组,所以没有必要将这个数组传递给你的任何函数,因为它们可以访问它 - 当你处理数组和它们的索引时,没有必要使用if语句来弄清楚客户在说什么;我们可以使用choice - 1作为数组的索引(在阅读代码后您将知道我在谈论什么)
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
// Structure to hold information about drink
struct Drink
{
string drinkName;
double cost;
int numberInMachine;
};
// Essentially the machine with information about what is in it
Drink options[] = {{"Cola", .75, 0}, {"Root Beer", .75, 2}, {"Lemon-Lime", .75, 10},{"Grape Soda", .80, 3}, {"Cream Soda", .80, 20}};
int getChoice();
double showTransaction(int);
int main()
{
int choice;
double moneyEarned = 0.0;
// Figuring out what the cutomer chose
choice = getChoice();
// Figuring out how much money the machine earned
moneyEarned = showTransaction(choice);
cout << "The machine earned: $" << moneyEarned << "." << endl;
return 0;
}
int getChoice()
{
int choice;
cout << "Enter the number(1-6) of the drink you would like: " << endl;
cout << "Drink Name Cost Number in Machine " << endl;
cout << "1. Cola .75 " << options[0].numberInMachine << endl;
cout << "2. Root Beer .75 " << options[1].numberInMachine << endl;
cout << "3. Lemon-lime .75 " << options[2].numberInMachine << endl;
cout << "4. Grape Soda .80 " << options[3].numberInMachine << endl;
cout << "5. Cream Soda .80 " << options[4].numberInMachine << endl;
cout << "6. Quit " << endl;
cout << "Enter the number of your selection: ";
cin >> choice;
while(choice < 1 || choice > 6)
{
cout << "Please enter a valid number 1-6" << endl;
cin >> choice;
}
return choice;
}
double showTransaction(int choice)
{
double moneyIn;
// If there isn't enough drinks ie. more than 0, then we can't sell any
if(options[choice-1].numberInMachine < 1)
return 0.0;
cout << options[choice-1].drinkName << "costs $" << options[choice-1].cost << "." << endl;
cout << "Enter money inserted up to $1.00: ";
cin >> moneyIn;
// If they enter less money than we need
while(moneyIn < options[choice-1].cost)
{
cout << "The entered money is not enough, Please enter more: ";
cin >> moneyIn;
}
cout << "Your change is: $" << (moneyIn - options[choice-1].cost) << "." << endl;
return moneyIn;
}
相关文章:
- 如何正确地将分支添加到已存在的树中
- 如何正确地将带有指针的数组传递给函数
- 如何正确地推回然后遍历堆中对象的向量?
- 我是否不正确地集中了这些字符数组?
- 如何通过另一个对象中的命令正确地从一个对象返回数据
- 我怎样才能通过友谊正确地履行我的职能?
- C++:如何正确地将 deque::front() 返回的变量从函数中传递出去?
- 我应该如何正确地将packaged_task移动到 lambda?
- 如何正确地将SHA1CryptoServiceProvider转换为c ++?
- 如何正确地对 constexpr 函数进行单元测试
- 我的动态链接队列在同一输出流中调用时不正确地输出三个返回函数
- 如何正确地将boost::asio::buffers_iterator转换为InputIterator
- 如何正确地检查变量char中包含的字母
- 当试图交换可变模板类时,如何正确地重载operator=
- 如何正确地将 boost::optional<std::chrono::d uration> 作为函数参数?
- 如何在C++中正确地将我的语言字符输出到HTML文件中
- 如果我的类正确地管理了一个资源,那么拥有智能的poointer有什么意义
- 如何正确地编写这些条件,以便它们在控制台中工作
- 正确地编写一个类,并将pthread与vlc库和c++一起使用
- 如何正确地从函数 (type_traits) 给出向量