数组中的元素不会更改C

Elements in an array will not change c++

本文关键字:元素 数组      更新时间:2023-10-16
   void Remove(int x)//x is the number that i want to remove
    {
        for(int i=0;i<CAPACITY;i++)//loop is to find the first case of x
        {
             if(x==data[i])//if x is in data 
             {
                  cout<<data[i]<<endl;//for debugging
                  data[i]==0; //change x to 0
                  cout<<data[i]<<endl;
             }
        }
     }

当我表明它是否有效时,我想删除的数字仍然存在。这是当我运行x = 15时输出之前的输出:
12,15,20,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
我使用COUT查看条件是否存在问题,但是如果x在数组中,则运行。这是之后的输出,即使x在数组中:
12,15,20,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

在您的循环中使用comparison operator '==',因此仅放置一个 =,这意味着您将变量x分配给数据数组。

行中的问题data[i]==0; //change x to 0

== 是一个比较操作员。为了分配一个值,请改用=。所以:

data[i] = 0