2016-11-16 58 views
0

我正在尝试保持一杯咖啡已售出,我必须使用用户定义的功能才能完成此操作。我已经尝试了附加代码的众多变体,但似乎没有任何工作。我究竟做错了什么?我也是C++的新手,所以这就是为什么它看起来业余!如何使用用户定义函数在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; 

} 
+0

那么,目前的代码使用例如, 'smCup'有两个不同的用途:将当前的销售记录到一个给定的客户,并跟踪销售的小杯的总数。这些用途有冲突。你需要积累在不同的变量中。某些函数中的局部变量将不会执行。它们仅在每个函数调用期间存在。 –

+0

首先,您必须确定哪些变量是“总计”,哪些是“当前销售额”。你似乎混淆了它们。您应该在实际进行销售时调用函数_update_ totals,而不是在报告时调用。您需要将值作为参数传递给该函数,而不仅仅是将它们定义并将它们设置为零。 – paddy

回答

1

所以你可能知道,你没有保持跟踪,你卖(即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。希望这可以帮助!

编辑:无法评论,所以我只能说你不客气!

0

谢谢KTing!知道我更接近于我的代码的早期版本的正确答案是令人失望的。我无法弄清楚为什么它不会初始化,所以我开始绝望,尝试我95%肯定不会工作的东西。我结束了以下解决方案。

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

using namespace std; 

//Constant for size of cup of coffee 
const int SM_OZ = 8; 
const int MD_OZ = 12; 
const int LG_OZ = 16; 

//Constant for price of cup of coffee and Tax 
const double SM_PRICE = 1.19; 
const double MD_PRICE = 1.49; 
const double LG_PRICE = 1.89; 
const double TAX = .0825; 

int main() 
{ 
    //declare and initialize the variables for the individual cups of coffee 
    int selection; 
    int smCup = 0; 
    int mdCup = 0; 
    int lgCup = 0; 

    //declare and initialize the variables for the total cups of coffee 
    int smtCup = 0; 
    int mdtCup = 0; 
    int lgtCup = 0; 

    do 
    { 
     //get input from user as to what they want to 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: 
      //get the number of cups of coffee from the user 
      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; 

      //get the total cups of coffee and store it as a variable 
      smtCup += smCup; 
      mdtCup += mdCup; 
      lgtCup += 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; 
      cout << "SIZE" << setw(21) << "Number" << endl; 
      cout << "Small: " << setw(18) << smtCup << endl; 
      cout << "Medium: " << setw(17) << mdtCup << endl; 
      cout << "Large: " << setw(18) << lgtCup << 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) << smtCup << setw(18) << smtCup*SM_OZ << endl; 
      cout << "Medium: " << setw(17) << mdtCup << setw(18) << mdtCup*MD_OZ << endl; 
      cout << "Large: " << setw(18) << lgtCup << setw(18) << lgtCup*LG_OZ << endl; 
      cout << "Total: " << setw(36) << (smtCup*SM_OZ) + (mdtCup*MD_OZ) + (lgtCup*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) << smtCup << setw(18) << SM_PRICE << setw(18) << smtCup*SM_PRICE << endl; 
      cout << "Medium: " << setw(17) << mdtCup << setw(18) << MD_PRICE << setw(18) << mdtCup*MD_PRICE << endl; 
      cout << "Large: " << setw(18) << lgtCup << setw(18) << LG_PRICE << setw(18) << lgtCup*LG_PRICE << endl; 
      cout << "Subtotal: " << setw(51) << (smtCup*SM_PRICE) + (mdtCup*MD_PRICE) + (lgtCup*LG_PRICE) << endl; 
      cout << "Tax: (8.25%)" << setw(49) << ((smtCup*SM_PRICE) + (mdtCup*MD_PRICE) + (lgtCup*LG_PRICE))*TAX << endl; 
      cout << "Total: " << setw(54) << ((smtCup*SM_PRICE) + (mdtCup*MD_PRICE) + (lgtCup*LG_PRICE)) + (((smtCup*SM_PRICE) + (mdtCup*MD_PRICE) + (lgtCup*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; 

     } 

    //loop through if the user is still making a valid selection 
    } while (selection != 0); 

    system("pause"); 
    return 0; 

} 
相关问题