C++ 自动售货机程序的更改值不正确
main.cpp:C++ 自动售货机程序的更改值不正确,c++,C++,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 Availabl
#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);
}
#包括
#包括
#包括“Money.h”;
#包括“Product.h”;
使用名称空间std;
int main(){
字符串pName;//productName
双pAmount,pPrice;//productAmount
cout您不必在任何地方初始化double change变量,产品价格基本上也是如此
此处未使用pPrice变量
Money::Money(double amountP, double pPrice) {
...
}
你应该调试你的程序。打印一些调试消息来了解正在发生的事情。当你发现错误行时,它通常会很快被诊断出来。我猜你有一个未初始化的变量,或者一些溢出,或者指针/值混淆。但这只是我的想法,我没有看代码。你真的想使用floatin吗货币值的g点数字?这不起作用,因为二进制浮点数不能精确表示1/100(请参阅和)。唯一正确的方法是在打印时使用以美分为单位的整数值,并仅使用除法。另一个问题似乎是您默认初始化了成员,而基元类型的默认初始化使其未初始化。您希望对成员进行值初始化。另一方面,您应该将输出作为文本复制到此处。这样会更易于使用。您可以看到,windows控制台可以进行复制和粘贴(通过菜单或启用“快速编辑”,而不是鼠标右键).double change;是用金钱初始化的。h.另外,未使用的double pPrice不应该导致随机数出现。有很多额外的东西,因为我试图弄清楚代码是怎么回事
#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;
}
}
#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
#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;
}
}
Money::Money(double amountP, double pPrice) {
...
}