Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

(C++)Why does this code shows a run time error ? I would like some suggestions, and corrections.Thanks #define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include #include

(C++)Why does this code shows a run time error ? I would like some suggestions, and corrections.Thanks

#define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include #include // :library used to define and access various properties of numerical // data types //(such as the maximum and minimum values)

using namespace std;

class Snack { private: string snackName; double priceSnack; int snackQuantity; int sold; string* transaction; 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 snackName = ""; priceSnack = 0; snackQuantity = 0; sold = 0; transaction = nullptr; } Snack(string name, double price, int quant)//Overload cosntructor { snackName = name; priceSnack = price; snackQuantity = quant; transaction = new string[quant]; for (int i = 0; i < quant; i++) transaction[i] = "";

} // Creating accessor methods void setName(string s) { if (s.length() <= 0) { cout << "Please enter a name" << endl; } else snackName = s; } void setPrice(double pr) { if (pr <= 0) { cout << "Please enter a price greater than 0" << endl; } else priceSnack = pr; }

void setQuantity(int qt) { if (qt <= 0) { cout << "Please enter number greater than 0" << endl; } else { snackQuantity = qt; transaction = new string[qt]; for (int i = 0; i < qt; i++) transaction[i] = ""; } } // Creatting getters string getName() const { return snackName; } int getInStock() const { return snackQuantity; } int getSold() const { return sold; } double getPrice() const { return priceSnack; }

bool buyItem(double& moneyEntered) { if (snackQuantity == 0) { cout << "Error: item is sold-out" << endl; return false; } if (moneyEntered >= priceSnack) { moneyEntered -= priceSnack; transaction[sold] = getCurrentTime(); snackQuantity--;

sold++; cout << "Item has been dispensed below" << endl; return true; } else if (moneyEntered < priceSnack) { if (moneyEntered < 0) { cout << "Please enter a number greater than 0" << endl; } else { cout << "Amount is less than the price $" << priceSnack << endl; } return false; } } ~Snack() // Destructor { if (sold) { cout << " Closing details for " << snackName << " "; cout << " \tNumber of sold: " << priceSnack; cout << " \tProfit Gained: " << priceSnack * snackQuantity << " "; for (int i = 0; i < sold; i++) { cout << "\t" << transaction[i] << endl; } } delete[] transaction; transaction = nullptr; cout << endl;

}

}; void displayVendingMachine(const Snack[], const int); int getQuarters(); void userBuyItem(Snack[], const int); bool promptToContinue();

int main() { const int SNACK_MAX = 3;

Snack snack[SNACK_MAX] = { Snack(), Snack("Candy", 1.25,5), Snack("Soda",1.00,2) }; snack[0].setName("Chips"); snack[0].setPrice(1.75); snack[0].setQuantity(3);

cout << "Welcome to the vender machine! "; do { userBuyItem(snack, SNACK_MAX); } while (promptToContinue()); }

// Function for formatting void displayVendingMachine(const Snack snacks[], int size) { cout << "Welcome to the Snack Vending Machine!" << endl; cout << setw(54) << setfill('-') << "-" << setfill(' ') << endl; cout << setw(5) << "Item #" << setw(20) << "Item Name" << setw(13) << "Price" << setw(14) << "# in Stock" << endl; cout << setw(54) << setfill('-') << "-" << setfill(' ') << endl; for (int i = 0; i < size; i++) { cout << setw(5) << i + 1 << setw(20) << snacks[i].getName() << setw(10) << "$" << fixed << setprecision(2) << snacks[i].getPrice() << setw(10) << snacks[i].getInStock(); cout << endl; } cout << setw(54) << setfill('-') << "-" << setfill(' ') << endl; cout << endl; }

// Function to validate the quarters int getQuarters() { int quarter;

// Continuously prompt the user until they enter a valid input while (true) { cout << "Enter a number of quarters: "; cin >> quarter;

// Check if the user entered an invalid input (not a positive integer) if (cin.fail() || quarter < 1) { cout << "Invalid input. Enter a positive integer value greater than or" << " equal to 1: ";

// Clear the error flags in the cin stream cin.clear(); // Ignore any remaining characters in the input buffer

cin.ignore(numeric_limits::max(), ' '); continue; }

// If a valid input was entered, exit the loop break; }

return quarter;

}

void userBuyItem(Snack snack[], const int size) { displayVendingMachine(snack,size); int quarter = getQuarters(); int userInput; double money; cout << "Amount entered: " << quarter * 0.25 << endl; money = quarter * 0.25; while (true) { cout << endl << "Enter a number between 1 and " << size << " to make your selection: "; cin >> userInput; if (cin.fail() || userInput < 1 || userInput > size) { cin.clear(); cin.ignore(numeric_limits::max(), ' '); cout << "Error: enter a number between 1 and " << size << ": "; continue; } break; } snack[userInput - 1].buyItem(money);

if (money > 0) cout << "$" << money << " dispensed "; } bool promptToContinue() { char choice; cout << endl << "Continue? (Y / N): "; cin >> choice; if (choice == 'Y' || choice == 'y') return true; return false; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions