C++ C++;-如何在不使用全局变量的情况下从函数调用变量

C++ C++;-如何在不使用全局变量的情况下从函数调用变量,c++,function,variables,global,C++,Function,Variables,Global,我一直在尝试完成一项任务,但不允许使用全局变量 如果您必须制作一个咖啡店程序,用户可以在其中购买咖啡(小型、中型或大型),则显示售出的咖啡杯总数,然后显示售出咖啡的总销售额。我完成了用户可以购买的代码的一半,但我一直在计算咖啡销售额/杯数 函数cupsSold和totalAmount返回0或垃圾值。此外,我不允许使用额外的库函数 有没有办法不使用全局变量就从另一个函数调用变量 这是我的主要代码: #include <iostream> using namespace std; vo

我一直在尝试完成一项任务,但不允许使用全局变量

如果您必须制作一个咖啡店程序,用户可以在其中购买咖啡(小型、中型或大型),则显示售出的咖啡杯总数,然后显示售出咖啡的总销售额。我完成了用户可以购买的代码的一半,但我一直在计算咖啡销售额/杯数

函数cupsSold和totalAmount返回0或垃圾值。此外,我不允许使用额外的库函数

有没有办法不使用全局变量就从另一个函数调用变量

这是我的主要代码:

#include <iostream>
using namespace std;

void showChoices()
{
    cout << "Welcome to Jason's Coffee Shop\n\n";
    cout << "1. Buy Coffee" << endl;
    cout << "2. Show total cups sold by size"<< endl;
    cout << "3. Show total amount of coffee sold"<<endl;
    cout << "4. Show total amount of money made"<< endl;
    cout << "0. Exit" << endl;
    cout << "\nYour choice: ";
}

void buyCoffee(int numberSmCups,float totalSmCups,int numberMedCups,float totalMedCups, int numberLgCups,float totalLgCups)
{

    char coffeeSize, order = 'y';
    bool coffeeSelect = true;
    float smCoffee = 1.75, mdCoffee = 1.90, lgCoffee = 2.00, totalCoffee = 0;

    while(coffeeSelect)
    {        
        if (order == 'y' ||order =='Y'){

        cout << "Size of Coffee [S, M, L]: ";
        cin >>  coffeeSize;

        if (coffeeSize == 's' || coffeeSize == 'S')
        {
            cout << "Quantity of Small Coffee: ";
            cin >> numberSmCups;
            totalSmCups = numberSmCups * smCoffee;
            cout << "Small Coffee: "<<numberSmCups<<", Bill: "<<totalSmCups<<endl;
            totalCoffee += totalSmCups;
            cout << "Add another coffee [Y or N]: "<<endl;
            cin >>  order;
        }
        else if (coffeeSize == 'm' || coffeeSize == 'M')
        {
            cout << "Quantity of Medium Coffee: ";
            cin >> numberMedCups;
            totalMedCups = numberMedCups * mdCoffee;
            cout << "Medium Coffee: "<<numberMedCups<<", Bill: "<<totalMedCups<<endl;
            totalCoffee += totalMedCups;
            cout << "Add another coffee [Y or N]: "<<endl;
            cin >>  order;
        }
        else if (coffeeSize == 'l' || coffeeSize == 'L')
        {
            cout << "Quantity of Large Coffee: ";
            cin >> numberLgCups;
            totalLgCups = numberLgCups * lgCoffee;
            cout << "Large Coffee: "<<numberLgCups<<", Bill: "<<totalLgCups<<endl;
            totalCoffee += totalLgCups;
            cout << "Add another coffee [Y or N]: ";
            cin >>  order;
        }
        }
        else
            break;
    }

    cout << "--------------\n";
    cout << "Your invoice: "<<endl;
    cout<< endl;
    if (numberSmCups>= 1) 
        cout << "Small Coffee: "<< numberSmCups <<" cups ($ "<< smCoffee <<"/cup) Amount: $ "<< totalSmCups << endl;
    if (numberMedCups>=1)
        cout << "Medium Coffee: "<< numberMedCups <<" cups ($ "<< mdCoffee << "/cup) Amount: $ "<< totalMedCups << endl;
    if (numberLgCups>=1) 
        cout << "Large Coffee: " << numberLgCups <<" cups ($ " << lgCoffee << "/cup) Amount: $ " << totalLgCups << endl;
    cout<< endl;
    cout << "Total Amount: "<<"$ "<< (totalSmCups+ totalMedCups+ totalLgCups) << endl;
    cout << "--------------" << endl;

 }

void cupsSold(int numberSmCups,int numberMedCups,int numberLgCups){
     int lifeSmCups=0;
     int lifeMdCups=0;
     int lifeLgCups=0;

     lifeSmCups = lifeSmCups + numberSmCups;
     lifeMdCups = lifeMdCups + numberMedCups;
     lifeLgCups = lifeLgCups + numberLgCups;

     cout << "Total Small cups: "<<lifeSmCups<<endl;
     cout << "Total Medium Cups:"<<lifeMdCups<<endl;
     cout << "Total Large Cups:"<<lifeLgCups<<endl;
}

void totalAmount(float totalCoffee)
{
    cout << "Total: "<<"$"<<totalCoffee<<endl;
}

int main()
{
    int numberSmCups = 0, numberMedCups = 0, numberLgCups = 0, choice;
    float totalSmCups = 0, totalMedCups = 0, totalLgCups = 0, totalCoffee;

    while (choice != 0)
    {
        showChoices();
        cin >> choice;
        cout << endl;

        if (choice == 1)
        {
            cout <<"You selected to buy coffee."<< endl;
            buyCoffee(numberSmCups,totalSmCups,numberMedCups,totalMedCups,numberLgCups,totalLgCups);
        }
        else if (choice == 2)
        {
            cout << "The total amount of cups sold by size is:  "<<endl;
            cupsSold(numberSmCups,numberMedCups,numberLgCups);
        }
        else if (choice == 3)
        {
            cout << "Total Coffee Sold:  "<<endl;
           // cupsSold(numberSmCups,numberMedCups,numberLgCups);
        }
        else if (choice == 4)
        {
            cout << "Money made from coffee sales:  "<<endl;
            totalAmount(totalCoffee);
        }
        else if (choice == 0)
        {
            cout << "EXIT"<<endl;
            break;
        }
        else
            cout << "Invalid Input" << endl;
    }

    return 0;
}
#包括
使用名称空间std;
void showChoices()
{
库特
“有没有办法从另一个函数调用变量而不使用全局变量?”

如果要更改传递给这样一个函数的变量,则需要使用by-reference参数

void cupsSold(int& numberSmCups,int& numberMedCups,int& numberLgCups){
              // ^                 ^                  ^
否则,这些函数只对副本而不是原始变量值进行操作

“有没有办法从另一个函数调用变量而不使用全局变量?”

如果要更改传递给这样一个函数的变量,则需要使用by-reference参数

void cupsSold(int& numberSmCups,int& numberMedCups,int& numberLgCups){
              // ^                 ^                  ^

否则,这些函数只对副本而不是原始变量值进行操作。

太多不相关的代码。请提供一个复制您的实际问题的代码!太多不相关的代码。请提供一个复制您的实际问题的代码!我确实使用了参考参数。我运行程序,购买咖啡,然后显示售出的杯子数量。我可以看到售出的杯子总数,但当我再次按选择1购买咖啡,然后按查看时,总和从0开始。我用参考参数编辑了代码。我确实使用了参考参数。我运行程序,购买咖啡,然后显示售出的杯子数。我可以看到售出的杯子总数,但当我再次按选择1购买咖啡时在和视图中,求和从0开始。我用引用参数编辑了代码。