-1

I am writing a "vending machine/grocery shopping" code in C++ where I have a menu of 5 items and the user can choose to add as my items as they want. The price is calculated at the end.

Since the user can add as many items they want, I used a while loop to so they can "continue shopping." However, I was not able to successfully do this because they code would keep running. I tried to put the switch statement in a function but did not call the "reply" properly.

Could someone help me with this code, specifically the while loop and switch statement function called int shoppingCart(). If someone could help me with abstracting this code that would be great!)

Code below (this is the original, I put an edited one below):

#include <iostream>
using namespace std;


void vendingMachine() {
  cout << "1. Popcorn: $2" << endl;
  cout << "2. Coconut Clusters: $3" << endl;
  cout << "3. Granola Bar: $2.50" << endl;
  cout << "4. Trail Mix: $1.50" << endl;
  cout << "5. Chocolate: $1" << endl;
  cout << "Press 0 to checkout" << endl;
}

int main() {
  cout << "Vending Machine" << endl;
  cout << "----Items------" << endl;

  vendingMachine();

  
  cout << "Enter you selection: " << flush;
  int input;
  cin >> input;
  float cost;

  switch (input) {
  case 1:
    cout << "You added Popcorn to your cart." << endl;
    cost = 2;
    break;
  case 2:
    cout << "You added Coconut Clusters to your cart." << endl;
    cost = 3;
    break;
  case 3:
    cout << "You added Granola Bar to your cart." << endl;
    cost = 2.50;
    break;
  case 4:
    cout << "You added Trail Mix to your cart." << endl;
    cost = 1.50;
    break;
  case 5:
    cout << "You added Chocolate to your cart." << endl;
    cost = 1;
    break;
  case 6:
    cout << "Checkout" << endl;
    break;
  default:
    cout << "Please select an item from the menu" << endl;
  }
  cout << "Continue shopping (y/n): " << flush;
  string reply;
  cin >> reply;


  while(reply == "y") {
    cout << "Enter your selection: " << flush;
    int input;
    cin >> input;
    float cost;

    switch (input) {
    case 1:
      cout << "You added Popcorn to your cart." << endl;
      cost = 2;
      break;
    case 2:
      cout << "You added Coconut Clusters to your cart." << endl;
      cost = 3;
      break;
    case 3:
      cout << "You added Granola Bar to your cart." << endl;
      cost = 2.50;
      break;
    case 4:
      cout << "You added Trail Mix to your cart." << endl;
      cost = 1.50;
      break;
    case 5:
      cout << "You added Chocolate to your cart." << endl;
      cost = 1;
      break;
    case 6:
      cout << "Checkout" << endl;
      break;
    default:
      cout << "Please select an item from the menu" << endl;
    }
    cout << "Continue shopping (y/n): " << flush;
    string reply;
    cin >> reply;
    break;
  }
  cout << "Proceding to checkout..." << endl;
  



  

  cout << "Pay amount: $" << flush;
  float money;
  cin >> money;

  if (money > cost) {
    float change = money-cost;
    cout << "Thank you! You have $" << change << " change." << endl;
  }

  if (money == cost) {
    cout << "Thank you! Have a nice day!." << endl;
  }

  if (money < cost) {
    float amountOwed = cost-money;
    cout << "Please insert another $" << amountOwed << endl;

    cout << "Enter amount: " << flush;
    float payment;
    cin >> payment;

    if (payment > amountOwed) {
    float change2 = payment-cost;
    cout << "Thank you! You have $" << change2 << " change." << endl;
    }

    if (payment == amountOwed) {
      cout << "Thank you! Have a nice day!." << endl;
    }

    if (payment < amountOwed) {
      cout << "Sorry, you did not enter enough money. Your cart has emptied." << endl;
    }

  }
  return 0;
}

Edited code:

#include <iostream>
using namespace std;


void vendingMachine() {
  cout << "1. Popcorn: $2" << endl;
  cout << "2. Coconut Clusters: $3" << endl;
  cout << "3. Granola Bar: $2.50" << endl;
  cout << "4. Trail Mix: $1.50" << endl;
  cout << "5. Chocolate: $1" << endl;
  cout << "Press 0 to checkout" << endl;
}

int processSelection() {
  cout << "Enter your selection: " << flush;
  int input;
  cin >> input;
  
  return input;
}

int shoppingCart() {
  int selection = processSelection();
  float cost;
  switch (selection) {
  case 1:
    cout << "You added Popcorn to your cart." << endl;
    cost = 2;
    break;
  case 2:
    cout << "You added Coconut Clusters to your cart." << endl;
    cost = 3;
    break;
  case 3:
    cout << "You added Granola Bar to your cart." << endl;
    cost = 2.50;
    break;
  case 4:
    cout << "You added Trail Mix to your cart." << endl;
    cost = 1.50;
    break;
  case 5:
    cout << "You added Chocolate to your cart." << endl;
    cost = 1;
    break;
  case 6:
    cout << "Checkout" << endl;
    break;
  default:
    cout << "Please select an item from the menu" << endl;
  }
  cout << "Continue shopping (y/n): " << flush;
  string reply;
  cin >> reply;

  return reply;
}

int main() {
  cout << "Vending Machine" << endl;
  cout << "----Items------" << endl;

  vendingMachine();

  int reply = shoppingCart();

  float cost;
  

  


  while(reply == "y") {
    processSelection();

    shoppingCart();
  }
  cout << "Proceding to checkout..." << endl;
  



  

  cout << "Pay amount: $" << flush;
  float money;
  cin >> money;

  if (money > cost) {
    float change = money-cost;
    cout << "Thank you! You have $" << change << " change." << endl;
  }

  if (money == cost) {
    cout << "Thank you! Have a nice day!." << endl;
  }

  if (money < cost) {
    float amountOwed = cost-money;
    cout << "Please insert another $" << amountOwed << endl;

    cout << "Enter amount: " << flush;
    float payment;
    cin >> payment;

    if (payment > amountOwed) {
    float change2 = payment-cost;
    cout << "Thank you! You have $" << change2 << " change." << endl;
    }

    if (payment == amountOwed) {
      cout << "Thank you! Have a nice day!." << endl;
    }

    if (payment < amountOwed) {
      cout << "Sorry, you did not enter enough money. Your cart has emptied." << endl;
    }

  }
  return 0;
}
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
yeliah
  • 41
  • 6
  • 1
    `reply` in expressions `while(reply == "y")` and `string reply; cin >> reply;` are two different variables. – 273K Aug 02 '20 at 16:05

1 Answers1

4

It looks like you're making great progress learning to code. Some thoughts:

  1. Abstract duplicate code. Your switch statement is identical in two places. That makes it easy for bugs to appear! For example, if the price of one item changes, you might forget to update it in one place but not the other, which could lead to tricky bugs.
  2. You have a break statement at the end of the while loop. Do you want to break on every iteration? Probably not. When do you want to break out of the loop? Under what conditions do you not want to continue? Think about the control flow, and how while loops work. While some condition is true, continue looping. Once it's false, stop looping. What is that condition? Is it just reply == y? That works on the first iteration, what about other iterations?
  3. Some of you if statements say things like "have a good day." That sounds like a good time to break out of the loop to me. What do you think?
artoonie
  • 822
  • 5
  • 14
  • 1
    Since you already mentioned some good points you might want to add the foruth, that `string reply;` inside the `while` shadows the `string reply;` outside the `while` an thus updates the wrong variable. – Lukas-T Aug 02 '20 at 16:12
  • 1. How can i abstract the duplicate code? I tried putting it into a function but an error kept showing. 2. As mentioned in my question, I put in a break temporarily because my code kept repeating "enter your selection" and ignored the rest of the while loop. 3. How would i use parameters for my function? I think that would be the problem. – yeliah Aug 02 '20 at 16:20
  • Exactly, you want to put it inside a function! Make sure you give that function enough parameters and return values to be useful. If you get an error message, often times you can google that error and there will be a useful stackoverflow result right at the top. – artoonie Aug 02 '20 at 16:21