2016-11-16 17 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에 함수를 호출해야하며,보고 할 때가 아닙니다. 값을 정의하고 내부에서 0으로 설정하는 것뿐만 아니라 그 값을 함수의 인수로 전달해야합니다. – paddy

답변

1

그래서 당신은 아마 알고, 당신은 당신이 (즉 smtCup, mdtCup 및 lgtCup)를 판매하고있는 각 크기의 커피 잔의 수를 추적 아닙니다.

나는이 변수들이 각 크기에 대한 컵의 총 수를 의미한다고 가정하고 있습니다. 변수 선언 단계에서 몇 가지 의견을 달고 싶을 수도 있습니다. 이것은 매우 간단한 프로그램이기 때문에 당신이 당신의 amtSold 기능을 사용하지 않고 축적을 수행 할 수

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

을, 그래서 당신은 그것을 삭제할 수 있습니다 : 당신은 0으로 변수를 초기화 할 수 있습니다.

그런 다음 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; 

}