c++ - 如何正确传递这些值?

标签 c++ arrays structure

我应该使用一个结构来存储饮料名称、饮料成本和机器中的饮料数量。我应该创建一个包含五个结构的数组,其中的元素用名称、成本和数字初始化。该程序应显示饮料列表,用户应在第一个函数中进行选择(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;
    }

}

}

最佳答案

我已经修复了您代码中的一些问题。我已经评论了大部分;请通读它并尝试理解为什么我有我所做的。

主要注意事项有:

  1. 您声明了一个名为options 的全局数组,因此无需将此数组传递给您的任何函数,因为它们可以访问它
  2. 当您处理数组及其索引时,无需使用 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++ - 如何正确传递这些值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16305210/

相关文章:

c++ - 在没有安装 Visual Studio 的情况下编译 Visual Studio 项目?

python - Pandas 使用 numpy 数组填充值

python - 在 python 中交换 numpy 数组中的两行

c - 将 memcpy 与结构字段一起使用

excel - 我如何告诉 Matlab 正在导入的某些数据是十六进制的?

c++ - 从 void 指针到结构的深度复制到另一个变量

c++ - 帮助 C++ 中的 ifstream 函数

c++ - boost::asio 无法完全关闭 TCP 连接

c++ - std::bind 如何多次调用复制构造函数

python - 将函数与 numpy 数组的每个元素集成为集成限制