Question: so im heaving buggs in my code and if somebody can help fix them i'd much appreatie it! and you can not change anything in
so im heaving buggs in my code and if somebody can help fix them i'd much appreatie it! and you can not change anything in my code regarding all the funtions or classes or any other unless just for fixing a mistake and you hae to let me know wat you did so i can understadn where i missed up please pay attention to the output where it should be exactly the same as shown below pat attention to the in stock number and where on the second loop it crashes: thanks in advance and IF YOU DO NOT KNOW HOW TO DO IT PLS DONT DO IT Ill report you if you do it wrong n pls leave it to somebody who knows how to do it!
here is the output of how exactly it should be:
Item # Item Name Price # in - stock
- --------------------------------------------
1 Chips 1.75 3
2 Candy 1.25 5
3 Soda 1.00 2
Enter a number of quarters: - 1
Please enter a number greater than 0
Enter a number of quarters: 8
Amount Entered: $2.00
Enter a number between 1 and 3 to make your selection: 3
Item has been dispensed below
$1.00 dispensed below
Continue? (Y / N): y
Item # Item Name Price # in - stock
---------------------------------------------
1 Chips 1.75 3
2 Candy 1.25 5
3 Soda 1.00 1
Enter a number of quarters: 4
Amount Entered: $1.00
Enter a number between 1 and 3 to make your selection: 3
Item has been dispensed below
Continue? (Y / N): y
Item # Item Name Price # in - stock
---------------------------------------------
1 Chips. 1.75 3
2 Candy 1.25 5
3 Soda 1.00 0
Enter a number of quarters: 4
Amount Enter ed: $1.00
Enter a number between 1 and 3 to make your selection: 3
Error: item is sold - out!
$1.00 dispensed below
Continue? (Y / N): y
Item # Item Name Price # in - stock
1 Chips 1.75 3
2 Candy 1.25 5
3 Soda 1.00 0
Enter a number of quarters: 10
Amount Entered: $2.50
Enter a number between 1 and 3 to make your selection: 1
Item has been dispensed below
$0.75 dispensed below
Conti nue? (Y / N): y
Item # Item Name Price # in - stock
---------------------------------------------
1 Chips 1.75 2
2 Candy 1.25 5
3 Soda 1.00 0
Enter a number of quarters: 5
Amoun t Entered: $1.25
Enter a number between 1 and 3 to make your selection: 2
Item has been dispensed below
Continue? (Y / N): y
Item # Item Name Price # in - stock
---------------------------------------------
1 Chips 1.75 2
2 Candy 1.25 4
3 Soda 1.00 0
Enter a number of quarters: 9
Amount Entered: $2.25
Enter a number between 1 and 3 to make your selection: 1
Item has been dispensed below
$0.50 dispensed below
Continue? (Y / N) : n
Closing info for Soda:
0 in stock
2 sold for a profit of $2.00
Transactions occurred at:
00:25:55
00:26:03
Closing info for Candy:
4 in stock
1 sold for a profit of $1.25
Transactions occurred at:
00:26:24
Closing info for Chips:
1 in stock
2 sold for a profit of $3.50
Transactions occurred at:
00:26:17
00:26:38
and here is my code:
#include
#include
#include
#include
using namespace std;
class Snack {
private:
string snack_name;
double snack_price;
int numStock, numSold;
string *ptr;
string getCurrentTime() {
time_t t = time(0);
struct tm ts;
char buff[10];
ts = *localtime(&t);
strftime(buff, sizeof(buff), "%X", &ts);
return buff;
}
public:
Snack() { //default constructor
snack_name = "";
snack_price = 0.0;
numStock = 0;
numSold = 0;
}
Snack (string name, double price, int stock) {
snack_name = name;
snack_price = price;
numStock = stock;
numSold = 0;
ptr = new string[stock]; //dynamic array
}
bool buyItem(double& moneyEntered) {
string time;
bool check = true;
if (moneyEntered >= snack_price && numStock != 0) {
moneyEntered = moneyEntered - snack_price;
numStock -= 1;
numSold += 1;
cout << "Item has been dispensed below." << endl;
if (moneyEntered != 0) //prints out the 'change'
{
cout << " $" << moneyEntered << " dispensed below." << endl;
}
getCurrentTime();
return check;
}
else if (moneyEntered < snack_price) {
cout << "Error: Not enough money." << endl;
check = false;
return check;
}
else if (numStock == 0) {
cout << " Error: Item is sold out." << endl;
check = false;
cout << " $" << moneyEntered << " dispensed below." << endl;
return check;
}
return check;
}
void setTime (string time){
getCurrentTime() = time;
}
void setSnack(string name) {
snack_name = name;
}
void setPrice(double price) {
snack_price = price;
}
void setStock(int sto) {
numStock = sto;
}
string getName() const {
return snack_name;
}
double getPrice () const {
return snack_price;
}
int getStock () const {
return numStock;
}
~Snack() {
cout << " Closing info for " << snack_name << ":";
cout << endl << "\t" << numStock << " in stock" << endl;
cout << "\t" << numSold << " sold for a profit of $";
cout << static_cast(numSold)*snack_price << endl;
cout << "Transactions occurred at: " << endl;
cout << time;
}
};
void displayVendingMachine(const Snack[], int);
int getQuarters();
void userBuyItem(Snack[], int);
bool promptToContinue();
int main() {
const int ARRAY_SIZE = 3;
Snack inventory[ARRAY_SIZE] = { Snack(),
Snack("Candy", 1.25, 5),
Snack("Soda", 1.00, 2) };
inventory[0].setSnack("Chips");
inventory[0].setPrice(1.75);
inventory[0].setStock(3);
userBuyItem(inventory, 3);
return 0;
}
void displayVendingMachine(const Snack arr[], int size) {
cout << "Item #" << "\tItem Name" << "\tPrice" << "\t # in-stock";
cout << " ___________________________________________" << endl;
for (int i = 0; i < size; i++) {
cout << i + 1 << "\t" << arr[i].getName()
<< "\t\t" << fixed << setprecision(2)
<< arr[i].getPrice() << "\t\t"
<< arr[i].getStock() << endl;
}
}
int getQuarters() {
int numOfQuarters;
bool check = true;
double amount;
while(check) {
cout << " Enter a number of quarters: ";
cin >> numOfQuarters;
/* This prevents the program from going to an infinite loop if a user enters 4.0 */
cin.clear();
cin.ignore(numeric_limits::max(), ' ');
if (numOfQuarters < 1) {
cout << "Please enter a number greater than 0." << endl;
}
else
{
check = false;
}
}
return numOfQuarters;
}
void userBuyItem(Snack arr[], int size) {
int userInput, quarters;
double amount;
bool check = true;
displayVendingMachine(arr, size);
while (check)
{
quarters = getQuarters();
amount = 0.25*static_cast(quarters);
cout << "Amount entered: $" << amount << endl;
bool check2 = true;
while (check2) {
cout << " Enter a number between 1 and 3 to make your selection: ";
cin >> userInput;
if (userInput > 3 || userInput < 1) {
check2 = true;
cout << "Invalid selection. Please try again." << endl;
}
else {
check2 = false;
}
}
if(check) {
check = false;
arr[userInput-1].buyItem(amount);
cout << endl;
if (promptToContinue() == true) {
check = true;
displayVendingMachine(arr, size);
}
else {
check = false;
}
}
}
}
bool promptToContinue() {
char userInput;
bool check = true, loop = true;
while (loop) {
cout << "Continue? (y or n) ";
cin >> userInput;
if (userInput == 'y' || userInput == 'Y') {
check = true;
loop = false;
}
else if (userInput == 'n' || userInput == 'N') {
check = false;
loop = false;
}
else {
cout << "Please enter a 'y' for yes or 'n' for no." << endl;
}
}
return check;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
