-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得到:
为什么会出现这些随机数字?出于某种原因,productPrice被更改为-9******
。
你应该调试你的程序。打印一些调试消息以了解发生了什么。当您发现故障线路时,通常可以快速诊断。我猜你有一个未初始化的变量,或者一些溢出,或者一个指针/值混合。但那只是我的头顶,我没有看到代码。 – keyser
你真的想用货币值的浮点数吗?这是行不通的,因为二进制浮点数不能完全表达1/100(参见[this](http://floating-point-gui.de/)和[this](http://docs.oracle.com)的.com/CD/E19957-01/806-3568/ncg_goldberg.html))。唯一正确的方法是使用美分的整数值并仅用于打印。 –
另一个问题似乎是您默认 - 初始化您的成员,原始类型的默认初始化将其保留为未初始化状态。你想要初始化你的成员。 –