与c++中包含数组的if ledder混淆

Having confusion with if ledder containg array in C++

本文关键字:if ledder 混淆 数组 c++ 包含      更新时间:2023-10-16
#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 ",那么它们就都为真(使用"=="代替)!同样,对于您的变量,如果您没有将它们指定为公共,它们将默认为私有。