【发布时间】:2013-04-30 16:58:04
【问题描述】:
我应该使用一个结构来存储机器中的饮料名称、饮料成本和饮料数量。我应该创建一个由五个结构组成的数组,其中的元素用名称、成本和数字初始化。该程序应显示饮料列表,用户应在第一个功能中进行选择(1-6)。选择被验证并按值传递回主例程。在第二个功能中,用户插入货币并显示找零金额,并且应该从机器中的饮料数量中减去一个,然后循环。当用户退出时,它会显示机器赚取的总金额。我的问题是将数组传递给函数。我以为我做对了,但是函数和数组到处都出现错误。有谁知道我将如何传递数组并从函数返回值?谢谢
#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;
}
}
}
【问题讨论】:
-
您遇到了什么样的错误,您在哪一行代码中遇到了这些错误?
-
存在一些语法错误,例如:int getChoice(Drink,choice) {...} Drink 的变量是什么,choice 的类型是什么?
-
@Robert Harvey 在我尝试进入函数时,它说Drink 和choice 是未定义的。我在函数 int getChoice(Drink,choice) 中有错误。它所说的选择在 showTransaction 中未定义且相同。所以这是我通过它的方式
-
好的。请使您的标题更具体地针对您的特定问题;当前标题无法通过 Google 搜索。
-
是的,请更新