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;
}
}
输入 - &gt;水作为产品,数量为5.这是我得到的输出:
为什么我会得到这些随机数?由于某种原因,productPrice被更改为-9******
个数字。
答案 0 :(得分:2)
您不会在任何地方初始化双变量变量,本质上也是相同的产品价格。
您在此处未使用pPrice变量。
Money::Money(double amountP, double pPrice) {
...
}