2014-04-05 44 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:

#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。這是輸出I得到:

enter image description here

爲什麼會出現這些隨機數字?出於某種原因,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