If statement problem

Grettings I am currently trying to create a method that creates an IntSet that contains all the odd number froms another IntSet. My general idea is the following, if the item %2 != 0 then copy that item otherwise dont copy, however when I tried this it seems that my program ignores the if statement and just copies everything. My code is as follows, also if I take out the extra = in the link == NewNode statement it also doesnt work because it says that link cant be cahnged. Any help would be great.

IntSet IntSet::odd() const
// Member function
// PRE: (none)
// POST: A new IntSet is returned that contains only the odd elements
// (those whose values are odd numbers) of this IntSet
{

// this function needs to be implemented
IntSet temp( *this );
IntSetNode* ptr;
IntSetNode* newNode;


for( ptr = list; ptr != NULL; ptr = ptr->next )
{
if( ptr->item%2 != 0 )
{
newNode = new IntSetNode;
newNode->item = ptr->item;

// Insert it into the list
newNode->next = list;
list == newNode;
}
}

return temp;

}
Hello

1.
If you look at the right, you can find icon with "<>"symbol. This will make your source code look better
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
IntSet IntSet::odd() const
// Member function
// PRE: (none)
// POST: A new IntSet is returned that contains only the odd elements
// (those whose values are odd numbers) of this IntSet
{

  // this function needs to be implemented
  IntSet temp( *this );
  IntSetNode* ptr;
  IntSetNode* newNode;
  
  
  for( ptr = list; ptr != NULL; ptr = ptr->next )
  {
    if( ptr->item%2 != 0 ) 
    {
    newNode = new IntSetNode;
    newNode->item = ptr->item;
    
    // Insert it into the list
    newNode->next = list;
    list == newNode;
    }
  }
  
  return temp;

}




2.
At a glance I don't really see any part of the code that changes temp, which is the return value of your function. Because it is initialized by copying all elements from current IntSet that is calling the function, the returned IntSet is just same as it.
Last edited on
Topic archived. No new replies allowed.