2016-11-16 87 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; 

} 
相關問題