与c++中包含数组的if ledder混淆
Having confusion with if ledder containg array in C++
#include<iostream>
#include<conio.h>
using namespace std;
//const int n=20;
class cofee
{
//public:
int i;
//int n=20;
int price[20];
//int totalprice[n];
public:
void getprice()
{
if(i=1)
{
price[i]=10;
cout<<"price is :"<<price[i]<<""n;
}
else if(i=2)
{
price[i]=20;
cout<<"price is :"<<price[i];
}
else if(i=2)
{
price[i]=30;
cout<<"price is :"<<price[i];
}
else
{
cout<<"n";
}
}
void total()
{
int sum=0;
int n=3;
for(i=1;i<=n;i++)
{
sum = sum+price[i];
}
cout<<"total is:"<<sum;
}
};
int main()
{
cofee c;
int i;
do
{
cout<<"enter your choice";
cout<<"n1.plain cofee";
cout<<"n2.cold cofee";
cout<<"n3.farali cofee";
cout<<"n4.total";
cout<<"n5.quit";
cout<<"n";
cin>>i;
switch(i)
{
case 1 : c.getprice(); break;
case 2 : c.getprice(); break;
case 3 : c.getprice(); break;
case 4 : c.total(); break;
case 5 : break;
default : cout<<"nwrong cchoice";
}
}while(i!=5);
getch();
return 0;
}
我已经尝试了几乎所有我能,但仍然没有给出适当的输出。没有显示任何错误或警告。
in if else分类帐价格不存储值i=2 &3.需要帮助才能解决
当你在if中使用(=)时,你不会比较值,你实际上给了他值,所以第一个"if"将始终为真。如果它们都是" If "而不是"else If ",那么它们就都为真(使用"=="代替)!同样,对于您的变量,如果您没有将它们指定为公共,它们将默认为私有。
相关文章:
- 我的简单if-else语句是如何无法访问的代码
- 如何将enable-if与模板参数和参数包一起使用
- 无论条件是否为true,if总是在c++中执行
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- Insert函数不适用于2 if语句C++
- If语句未被求值C++
- C++嵌套if语句,基本货币交换
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 是否可以使用if constexpr删除控制流语句
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- 如何删除peer if else分支中的冗长句子
- 我似乎对if/else的基本语句有问题:/
- if数组上的随机数
- 将按位if条件转换为普通if条件
- If语句在c++中被忽略
- 比较if语句中的数组值和int值
- 使用if-else将数字转换为单词
- 为什么简单的算术减法在"if"条件下不起作用?
- 以在Qt中的IF语句中设置时间延迟
- 与c++中包含数组的if ledder混淆