2016-11-16 9 views
0

私は一杯のコーヒーを売り続けようとしています。私はそれを行うためにユーザー定義関数を使用しなければなりません。添付されたコードのさまざまなバリエーションを試しましたが、何も動作していないようです。私は間違って何をしていますか?また、私はC++のnewbですので、それが素人に見えます!ユーザ定義関数を使用して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; 

} 
関連する問題