Question
Hello, I asked another tutor to help me on this assignment and there were a few things I changed to the code. I need help
Hello, I asked another tutor to help me on this assignment and there were a few things I changed to the code. I need help with fixing one of the issues. When I type in an option for "Ice Tea" or "Ice Coffee", the option is not being recognized and gives me the error code. I'm not sure how to make this code recognize these options.
#include
using namespace std;
// class declaration ///////////////////////////////////////////////////
class SodaMachine { private: static const int NUMPRODUCTS = 6; string brand[NUMPRODUCTS]; int inStock[NUMPRODUCTS]; double price;
public: SodaMachine() { // default constructor
brand[0] = "Coke"; brand[1] = "Orange"; brand[2] = "Dew"; brand[3] = "Ice Tea"; brand[4] = "Water"; brand[5] = "Iced Coffee";
inStock[0] = 10; // Coke inStock[1] = 8; // Orange inStock[2] = 6; // Dew inStock[3] = 4; // Ice Tea inStock[4] = 2; // Water inStock[5] = 0; // Iced Coffee
price = 0.65;
}
string getBrand(int i) { // accessor: get subscript and return brand name return brand[i]; }
void setBrand(int i, string newBrand) { // mutator: replace old brand with new brand[i] = newBrand; }
int getInStock(int i) { // accessor: return number of stock of certain brand return inStock[i]; }
void decrementStock(int i) { // mutator: decrease stock for certain brand inStock[i] = inStock[i]-1; }
double getPrice() { // accessor: get price of soda return price; }
void setInStock(int i, int count) { // mutator: change stock number for brand inStock[i] = count; }
int totalCans() { // accessor: return total number of cans (followed screenshot) int total = 0; for(int i = 0; i < NUMPRODUCTS; i++) { total = total + inStock[i]; } return total; }
};
// declaration of other functions ///////////////////////////////////////////////////
void displayMenuChoice(SodaMachine * sm); // display list int errorChkChar(char choice, int flag, SodaMachine * sm); // include choice selection void displaySodas(SodaMachine * sm); // display list of sodas
// main function ///////////////////////////////////////////////////
int main() {
SodaMachine sm; // instantiation int flag = 1; char choice;
while(flag == 1) { displayMenuChoice(&sm); cout << "Enter your menu choice (A - C): "; cin >> choice; flag = errorChkChar(choice, flag, &sm); } }
// other functions ///////////////////////////////////////////////////
void displayMenuChoice(SodaMachine *sm){ cout << " Soda Machine Main Menu" << endl; cout << "\t ----------------------" << endl; cout << "\t A. Get Soda - $ " << sm -> getPrice() << endl; cout << "\t B. Restock Soda" << endl; cout << "\t C. Leave This Machine" << endl; cout << "\t ----------------------" << endl; }
int errorChkChar(char choice, int flag, SodaMachine *sm){ int sflag = 0; // soda flag char sodaC; int soda = 0; // soda number in list int same = 0; // for choice B check if input brand exists string sodaN; // for soda name
switch(choice){ case 'A': case 'a': displaySodas(sm);
while(sflag == 0) { cout << "Enter your choice: "; cin >> sodaC;
if(isdigit(sodaC)) { // check if input is a digit soda = int(sodaC) - 48; // convert char digit to int data type (-48 because ascii (48 ascii is value 0)) if(soda > 0 && soda <= 6) { if(sm -> getInStock(soda - 1) == 0) { cout << "No " << sm -> getBrand(soda-1) << " left. Make another selection." << endl; } else { sm -> decrementStock(soda - 1); cout << endl;//sm->getBrand(soda-1) << " purchased." << endl << endl; sflag = 1; } } else { cout << "Invalid input. Make another selection." << endl; } } else { cout << "Invalid input. Make another selection." << endl; } }
break;
case 'B': case 'b':
while(sflag == 0){ cout << "Enter the beverage name: "; cin >> sodaN; for(int i = 0; i < 6; i++){ if(strcasecmp(sodaN.c_str(), sm -> getBrand(i).c_str()) == 0){ // strcasecmp returns 0 if both equal sm -> setInStock(i, 20); same = 1; sflag = 1; cout << sm -> getBrand(i) << " is restocked with 20 cans." << endl << endl; } }
if(same == 0){ cout << sodaN << " is not on the list. Make a different selection." << endl; } }
break;
case 'C': case 'c': cout << "Total number of drinks: " << sm -> totalCans() << endl << endl; return 2; break;
default: cout << "Invalid choice. Please try again." << endl << endl; break; } return 1; }
void displaySodas(SodaMachine *sm) { cout << "\t-------------------------------" << endl; cout << "\tSoda"; cout << right << setw(15) << " Type "; cout<< "\t Stock" << endl; cout << "\t-------------------------------" << endl;
for(int i = 0; i < 6; i++){ cout << "\t " << i + 1 << "."; cout << right << setw(15) << sm -> getBrand(i); cout << "\t " << sm -> getInStock(i) << endl; } cout << endl; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started