gpt4 book ai didi

c++ - 如何使用用户定义的函数在 C++ 中创建累加器?

转载 作者:行者123 更新时间:2023-11-28 05:25:29 25 4
gpt4 key购买 nike

我正在尝试保持已售咖啡杯的运行总数,我必须使用用户定义的函数来执行此操作。我已经尝试了附加代码的多种变体,但似乎没有任何效果。我究竟做错了什么?另外,我是 C++ 的新手,所以它看起来很业余!

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

const int SM_OZ = 8;
const int MD_OZ = 12;
const int LG_OZ = 16;

const double SM_PRICE = 1.19;
const double MD_PRICE = 1.49;
const double LG_PRICE = 1.89;
const double TAX = .0825;

void amtSold(int &smtCup, int &mdtCup, int &lgtCup);

int main()
{
int selection;
int smCup;
int mdCup;
int lgCup;

int smtCup;
int mdtCup;
int lgtCup;

smCup = 0;
mdCup = 0;
lgCup = 0;


do
{
cout << "COFFEE SHOP" << endl;
cout << "1. Sell Coffee" << endl;
cout << "2. Total Number of Cups Sold" << endl;
cout << "3. Total Amount of Coffee Sold" << endl;
cout << "4. Total Amount of Money made" << endl;
cout << "0. Exit" << endl;
cout << "Type a number to continue: ";
cin >> selection;
cout << endl;


//loop through the solutions based on the user's selection
switch (selection)
{
case 1:
cout << "How many small cups of coffee: ";
cin >> smCup;
cout << "How many medium cups of coffee: ";
cin >> mdCup;
cout << "How many large cups of coffee: ";
cin >> lgCup;

system("cls");

cout << fixed << setprecision(2) << endl;

//Sale Coffee Receipt Page
cout << "COFFEE SHOP" << endl;
cout << "SIZE" << setw(21) << "Number" << setw(18) << "Price" << setw(18) << "Total" << endl;
cout << "Small: " << setw(18) << smCup << setw(18) << SM_PRICE << setw(18) << smCup*SM_PRICE << endl;
cout << "Medium: " << setw(17) << mdCup << setw(18) << MD_PRICE << setw(18) << mdCup*MD_PRICE << endl;
cout << "Large: " << setw(18) << lgCup << setw(18) << LG_PRICE << setw(18) << lgCup*LG_PRICE << endl;
cout << "Subtotal: " << setw(51) << (smCup*SM_PRICE)+(mdCup*MD_PRICE)+(lgCup*LG_PRICE) << endl;
cout << "Tax: (8.25%)" << setw(49) << ((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE))*TAX << endl;
cout << "Total: " << setw(54) << ((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE))+(((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE))*TAX) << endl;
cout << endl;
cout << endl;

break;

case 2:
//Total Number of Cups Sold
cout << "REPORT - NUMBER OF COFFEE CUPS SOLD" << endl;

amtSold(smtCup, mdtCup, lgtCup);
cout << "SIZE" << setw(21) << "Number" << endl;
cout << "Small: " << setw(18) << smCup << endl;
cout << "Medium: " << setw(17) << mdCup << endl;
cout << "Large: " << setw(18) << lgCup << endl;
cout << endl;
cout << endl;

break;

case 3:
//Total Amount of Coffee Sold
cout << "REPORT - AMOUNT OF COFFEE SOLD" << endl;

cout << "SIZE" << setw(21) << "Number" << setw(18) << "OZ" << endl;
cout << "Small: " << setw(18) << smCup << setw(18) << smCup*SM_OZ << endl;
cout << "Medium: " << setw(17) << mdCup << setw(18) << mdCup*MD_OZ << endl;
cout << "Large: " << setw(18) << lgCup << setw(18) << lgCup*LG_OZ << endl;
cout << "Total: " << setw(36) << (smCup*SM_OZ) + (mdCup*MD_OZ) + (lgCup*LG_OZ) << endl;
cout << endl;
cout << endl;

break;

case 4:
//Total Amount of Money made
cout << "COFFEE SHOP - REPORT MONEY MADE" << endl;

cout << "SIZE" << setw(21) << "Number" << setw(18) << "Price" << setw(18) << "Total" << endl;
cout << "Small: " << setw(18) << smCup << setw(18) << SM_PRICE << setw(18) << smCup*SM_PRICE << endl;
cout << "Medium: " << setw(17) << mdCup << setw(18) << MD_PRICE << setw(18) << mdCup*MD_PRICE << endl;
cout << "Large: " << setw(18) << lgCup << setw(18) << LG_PRICE << setw(18) << lgCup*LG_PRICE << endl;
cout << "Subtotal: " << setw(51) << (smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE) << endl;
cout << "Tax: (8.25%)" << setw(49) << ((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE))*TAX << endl;
cout << "Total: " << setw(54) << ((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE)) + (((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE))*TAX) << endl;
cout << endl;
cout << endl;

break;

case 0:

system("cls");

break;

default:
//notify the user that an invalid selection has been inputted
cout << "You have made an invalid selection. Please choose a number from the list." << endl;
cout << endl;

}

} while (selection != 0);


system("pause");
return 0;

}

void amtSold(int &smtCup, int &mdtCup, int &lgtCup)
{
int smCup;
int mdCup;
int lgCup;

smCup = 0;
mdCup = 0;
lgCup = 0;

smtCup += smCup;
mdtCup += mdCup;
lgtCup += lgCup;

}

最佳答案

因此,您可能知道,您没有跟踪销售的每种尺寸的咖啡杯(即 smtCup、mdtCup 和 lgtCup)的数量。

我假设这些变量表示每种尺码的总杯数,您可能想在变量声明步骤中添加一些注释。您需要将变量初始化为 0:

int smtCup = 0;
int mdtCup = 0;
int lgtCup = 0;

由于这是一个相当简单的程序,您可以在不使用 amtSold 函数的情况下执行累积,因此您可以删除它。

然后,在您的 switch 语句的情况 1 中,您将希望在每次更新值时更新 smtCup、mdtCup 和 lgtCup。请注意,smCup、mdCup 和 lgCup 仅用于此程序中的输入。

cout << "How many small cups of coffee: ";
cin >> smCup;
cout << "How many medium cups of coffee: ";
cin >> mdCup;
cout << "How many large cups of coffee: ";
cin >> lgCup;

smtCup += smCup;
mdtCup += mdCup;
lgtCup += lgCup;

从这里开始,你可以在其他情况下通过调用smtCup、mdtCup和lgtCup打印出小、中、大杯的总数!将案例2-4的smCup、mdCup、lgCup改为smtCup、mdtCup、lgtCup。希望这可以帮助!

编辑:无法发表评论,所以我只想说欢迎您来到这里!

关于c++ - 如何使用用户定义的函数在 C++ 中创建累加器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40624550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com