2014-04-05 3 views
-3

MAIN.CPP :잘못된 변경 값

#include <iostream> 
#include <string> 
#include "Money.h"; 
#include "Product.h"; 
using namespace std; 

int main() { 
    string pName; //productName 
    double pAmount, pPrice; //productAmount 
    cout << "Product's Available are ... " << endl; 
    cout << "---Water--- " << endl; 
    cout << "---Energy Drink--- " << endl; 
    cout << "---Thirst Quencher---" << endl; 
    cout << "---Protein Shake---" << endl; 
    cout << endl; 
    cout << "Please enter your selection" << endl; 
    //To trigger an exception, type in a product that is not stated on the list 
    cin >> pName; 
    Product p(pName); 
    cout << "Please enter the amount you are paying into the vending machine" << endl; 
    cin >> pAmount; 
    Money m(pAmount, p.productPrice); 

} 

Product.h :

#include <iostream> 
#include <string> 
using namespace std; 

#ifndef PRODUCT_H 
#define PRODUCT_H 

class Product { 
public: 
    Product(); 
    Product(string name); //default constructor 
    void givePrice(string Productname); 
//protected: 
    string productName; 
    double productPrice; 
}; 

#endif //PRODUCT_H 

Product.cpp :

#include <iostream> 
#include <string> 
#include "Product.h"; 

Product::Product(string name) { 
    givePrice(name); 
} 

Product::Product() {} 

void Product::givePrice(string productName) { 
    try { 


     if (productName == "Protein Shake" || productName == "protein shake") { 
      productPrice = 5; 
      cout << "The price for " << productName << " will be $" << productPrice << endl; 
      cout << endl; 
     } 

     else if (productName == "Water" || productName == "water") { 
      productPrice = 2.0; 
      cout << "The price for " << productName << " will be $" << productPrice << endl; 
      cout << endl; 
     } 

     else if (productName == "Energy Drink" || productName == "energy drink") { 
      productPrice = 4.25; 
      cout << "The price for " << productName << " will be $" << productPrice << endl; 
      cout << endl; 
     } 

     else if (productName == "Thirst Quencher" || productName == "thirst quencher") { 
      productPrice = 3.75; 
      cout << "The price for " << productName << " will be $" << productPrice << endl; 
      cout << endl; 
     } 

     else { 
      throw productName; 
     } 
    } catch (string x) { 
     cout << x << " does not exist! Please try again" << endl; 
     cout << endl; 
    } 

} 

Money.h :

#include <iostream> 
#include <string> 
#include "Product.h"; 
using namespace std; 

#ifndef MONEY_H 
#define MONEY_H 

class Money : public Product { 
public: 
    Money(double amountP, double pPrice); 
    void setChange(double& amount); 
    void addMoney(double& amount); 
protected: 
    bool insertMoney; 
    double amountPaid; 
    bool sufficientAmount; 
    double change; 
}; 

#endif //MONEY_H 

Money.cpp : - :

enter image description here

이유는 이러한 난수를 얻고있다

#include "Money.h" 
#include <iostream> 
using namespace std; 


Money::Money(double amountP, double pPrice) { 
    addMoney(amountP); 
    setChange(amountP); 
    cout << "Const: Value of PP = " << productPrice << endl; 
} 
void Money::addMoney(double& amount) { 
    amountPaid = amount; 
} 

void Money::setChange(double& amount) { 
    try { 
     sufficientAmount = false; 
     cout << "You have paid " << amountPaid << endl; //it always comes here 
     cout << "Current change value: " << change << endl; 
     cout << "Product Price: " << productPrice << endl; 
     while (sufficientAmount == false) { 
      if (amount < productPrice) { 
       cout << "You do not have enough money $" << change << " has been returned"; 
       sufficientAmount = false; 
      } 

      else if (amount > productPrice) { 
       //ness. Come again." << endl; doesnt come in here. 
       change = amountPaid-productPrice; //calculate change 
       cout << "Your change is $" << change << endl; 
       sufficientAmount = true; 
       cout << "Enjoy your product! Please come back again!" << endl; 
      } 

      else if (amount = productPrice) { 
       cout << "Thank you for your business. Come again and enjoy your drink."; 
       sufficientAmount = true; 
      } 





      else if (amount < 0 || amount == 0) { 
       throw 0; 
      } 

     } 
    } catch(int x) { 
     cout << "Please enter an amount that is higher than $" << x; 
    } 
} 

입력 제품으로> 물, 금액 5 등이 출력 내가 얻을 수있다? 어떤 이유로 든 productPrice가 -9****** 숫자로 변경됩니다.

+4

프로그램을 디버깅해야합니다. 몇 가지 디버그 메시지를 출력하여 현재 상황에 대한 느낌을 얻으십시오. 결함이있는 라인을 찾으면 신속하게 진단됩니다. 초기화되지 않은 변수 나 오버플로 또는 포인터/값 혼합이 있다고 생각합니다. 하지만 그건 내 머리 꼭대기에서 떨어져서, 나는 코드를 보지 않았다. – keyser

+2

정말 화폐 값에 부동 소수점 숫자를 사용하려고합니까? 이진 부동 소수점 숫자는 정확히 1/100을 표현할 수 없기 때문에 ([this] (http://floating-point-gui.de/) 및 [this] (http://docs.oracle.com/)을 참조하십시오. .com/cd/E19957-01/806-3568/ncg_goldberg.html)). 유일한 올바른 접근법은 센트에서 정수 값을 사용하고 인쇄용으로 만 나누는 것입니다. –

+0

다른 문제는 기본 멤버 초기화 및 기본 형식 초기화가 초기화되지 않은 상태로 유지되는 것입니다. 회원의 가치를 초기화하고 싶습니다. –

답변

2

이중 변경 변수를 어디에서나 초기화하지 않으며 기본적으로 제품 가격에 동일하게 적용됩니다.

여기서 pPrice 변수를 사용하고 있지 않습니다.

Money::Money(double amountP, double pPrice) { 
... 
} 
+0

두 번 변경; Money.h 초기화됩니다. 또한 이중 pPrice를 사용하지 않으면 난수가 나오지 않아야합니다. 코드로 어떤 일이 벌어 졌는지 알아 내려고했기 때문에 많은 추가 작업이있었습니다. – user3502316