2014-04-05 41 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