【问题标题】:Vending Machine C++ - Error in while loop自动售货机 C++ - while 循环中的错误
【发布时间】:2020-08-02 16:01:06
【问题描述】:

我正在用 C++ 编写“自动售货机/杂货店购物”代码,其中我有一个包含 5 个项目的菜单,用户可以根据需要选择添加为我的项目。最后计算价格。

由于用户可以添加任意数量的商品,因此我使用了while 循环,以便他们可以“继续购物”。但是,我无法成功地做到这一点,因为他们的代码会继续运行。我试图将 switch 语句放在一个函数中,但没有正确调用“回复”。

有人可以帮我处理这段代码,特别是 while 循环和称为 int shoppingCart()switch 语句函数。如果有人可以帮助我抽象出这段代码,那就太好了!)

下面的代码(这是原文,我在下面放了一个编辑过的):

#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;
}

修改后的代码:

#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;
}

【问题讨论】:

  • reply 表达式中的while(reply == "y")string reply; cin &gt;&gt; reply; 是两个不同的变量。

标签: c++ function while-loop abstraction


【解决方案1】:

看起来您在学习编码方面取得了很大进步。一些想法:

  1. 抽象重复代码。您的 switch 语句在两个地方是相同的。这使得错误很容易出现!例如,如果一件商品的价格发生变化,您可能会忘记在一个地方更新它而不是另一个地方,这可能会导致棘手的错误。
  2. 您在 while 循环的末尾有一个 break 语句。你想在每次迭代中中断吗?可能不是。当你想跳出循环?在什么情况下不想继续?想想控制流,以及 while 循环是如何工作的。当某些条件为真时,继续循环。一旦它是假的,停止循环。那是什么条件?只是reply == y?这适用于第一次迭代,那么其他迭代呢?
  3. 你们中的一些人如果陈述说“祝你有美好的一天”之类的话。对我来说,这听起来像是跳出循环的好时机。你怎么看?

【讨论】:

  • 既然您已经提到了一些优点,您可能需要添加正则表达式,即 while 内的 string reply; 会遮蔽 while 外的 string reply;,从而更新错误的变量。
  • 1.我怎样才能抽象出重复的代码?我尝试将其放入函数中,但一直显示错误。 2. 正如我的问题中提到的,我暂时休息一下,因为我的代码不断重复“输入您的选择”并忽略了 while 循环的其余部分。 3. 我将如何为我的函数使用参数?我认为这就是问题所在。
  • 没错,你想把它放在一个函数里面!确保为该函数提供足够的参数和返回值以使其有用。如果您收到一条错误消息,通常您可以通过谷歌搜索该错误,并且顶部会有一个有用的 stackoverflow 结果。
猜你喜欢
  • 1970-01-01
  • 2013-03-05
  • 2014-06-17
  • 1970-01-01
  • 1970-01-01
  • 2019-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多