我可以在“if 语句”中声明一个变量吗?

Can I declare a variable in an “if statement”?

我正在编写一个“自动售货机”程序,我需要 5 件商品,其中 3 件商品的价格为整数,另外 2 件的价格为小数。

我在此代码中只使用了 if 语句,但是我的 cost 变量有错误。

我做错了什么?

代码如下:

#include <iostream>
using namespace std;

int main() {

  cout << "Vending Machine" << endl;
  cout << "----Items------" << endl;
  cout << "1. Popcorn: " << endl;
  cout << "2. Coconut Clusters: " << endl;
  cout << "3. Granola Bar: .50" << endl;
  cout << "4. Trail Mix: .50" << endl;
  cout << "5. Chocolate: " << endl;

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


  if (input == 1) {
    cout << "You added Popcorn to your cart." << endl;
    float cost = 2;
    cout << "Your total is $" << cost << endl;
  }
  
  if (input == 2) {
    cout << "You added Coconut Clusters to your cart." << endl;
    float cost = 3;
    cout << "Your total is $" << cost << endl;
  }
  
  if (input == 3) {
    cout << "You added Granola Bar to your cart." << endl;
    float cost = 2.50;
    cout << "Your total is $" << cost << endl;
  }

  if (input == 4) {
    cout << "You added Trail Mix to your cart." << endl;
    float cost = 1.50;
    cout << "Your total is $" << cost << endl;
  }

  if (input == 5) {
    cout << "You added Chocolate to your cart." << endl;
    float cost = 1;
    cout << "Your total is $" << cost << 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;
}

当我尝试编译您的代码时,收到以下错误消息:

Error: ‘cost’ was not declared in this scope

为了修复此错误,您只需在 函数的主块 scope 中声明 cost 变量,例如:

int main()
{
    float cost;

    [...]
}

"Can I declare a variable in an “if statement”?"

是的。例如:

if (auto foo = bar())

变量 foo 将在 if 语句的主体内有效,并将被初始化为任何 bar() returns 和 [=13 的主体=] 将在 foo 转换为 truebool 值时输入。

从 C++17 开始,您还可以:

if (auto foo = bar(); foo == 42)

同样,变量 foo 将在 if 中声明并有效,并由 bar() 初始化,但您现在可以更好地控制何时 输入 if 正文。

更新:您刚刚更改了问题以在合适的位置添加float cost = 0;。现在您需要从每个 if (input == 块内的尝试分配中删除 float 关键字:例如将 float cost = 2.50; 更改为 cost = 2.50; 以便它更改函数范围 cost 变量,而不是创建额外的 if 范围变量。


正在解决您原来的问题...

你的 if 块...

if (input == 3) {
    cout << "You added Granola Bar to your cart." << endl;
    float cost = 2.50;
    cout << "Your total is $" << cost << endl;
}

...每个创建一个 float cost 变量局部于 if 的范围,因此在 } 之后,您设置的 cost 的值消失了.相反,您需要:

float cost;

在你的第一个 if (input == ... 语句之前,所以它的生命周期延伸到封闭函数范围的末尾(即直到 main() returns)。

问题是您正在执行以下操作:

int main()
{
    [...]
    if (input == 1) {
        cout << "You added Popcorn to your cart." << endl;
        float cost = 2;
        cout << "Your total is $" << cost << endl;
    }
    [...]
    if (money > cost) {
        [...]
    }
    [...]
}

变量 costscope 仅限于 if 块,因为那是你声明它的地方。因此,当您计算表达式 money > cost.

时,该变量不再存在

要解决此问题,您应该改为在函数的主块范围内声明变量 cost,如下所示:

int main()
{
    float cost;

    [...]
    if (input == 1) {
        cout << "You added Popcorn to your cart." << endl;
        cost = 2;
        cout << "Your total is $" << cost << endl;
    }
    [...]
    if (money > cost) {
        [...]
    }
    [...]
}