小编yel*_*iah的帖子

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

我正在编写一个“自动售货机”程序,我需要有 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 …
Run Code Online (Sandbox Code Playgroud)

c++ variables if-statement declaration

0
推荐指数
1
解决办法
136
查看次数

标签 统计

c++ ×1

declaration ×1

if-statement ×1

variables ×1