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

时间:2020-07-26 18:45:59

标签: c++ if-statement variables declaration

我正在编写一个“自动售货机”程序,我需要有5项,其中3项是整数,而另外2项是十进制。

我在此代码中仅使用if条语句,但是我的cost变量出错。

我做错了什么?

以下代码:

#include <iostream>
using namespace std;

int main() {

  cout << "Vending Machine" << endl;
  cout << "----Items------" << endl;
  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 << "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;
}

4 个答案:

答案 0 :(得分:5)

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

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) {
        [...]
    }
    [...]
}

变量cost的{​​{3}}被限制在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) {
        [...]
    }
    [...]
}

答案 1 :(得分:2)

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

Error: ‘cost’ was not declared in this scope

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

int main()
{
    float cost;

    [...]
}

答案 2 :(得分:2)

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

是的。例如:

if (auto foo = bar())

变量fooif语句的内部有效,并且将初始化为bar()返回的任何值,如果输入if的主体,则foo转换为bool的{​​{1}}值。

自C ++ 17起,您还可以执行以下操作:

true

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

答案 3 :(得分:2)

更新:您刚刚更改了问题,将float cost = 0;添加到了适当的位置。现在,您需要从每个float块内的尝试分配中删除if (input ==关键字:将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()返回)。

相关问题