简体   繁体   中英

Negative value not being added to Array

Problem

I have 2 arrays, one for positive numbers, one for negative numbers. For some reason, if the first number to be added is a negative number, it creates the array space to add the number yet the number inserted will always be 0.

Code for adding

Here is my add method, it determines if the value is negative or positive and adds the value to the appropriate array:

bool MyClass::addInt(int valueToBeInserted){

 if (valueToBeInserted >= 0){
    if (posArrayIterator >= sizeOfMyArray){
      return false;
    } else {
      cout << "added " <<  valueToBeInserted << "\n" << endl;
      myPArray[posArrayIterator] = valueToBeInserted;
      posArrayIterator ++;
      return true;
    } 
  } else {
    if (negArrayIterator >= sizeOfMyArray){
      return false;
    } else {
      cout << "added " <<  valueToBeInserted << "\n" << endl;
      myNarray[negArrayIterator] = valueToBeInserted;
      negArrayIterator ++;
      return true;
    }
  }

}

Output

With the following test:

b.addInt(-1);
b.addInt(-3);
b.addInt(-9);

The expected output would be

[-1, -3, -9] 

but output is

[-3, -9, 0].

Any help is much appreciated.

Since you are not posting the whole class I can only guess where the problem is.

1) Are posArrayIterator and negArrayIterator initialized to 0 ? They should be!

2) What is a value of sizeOfMyArray ?

There is nothing wrong with your bool MyClass::addInt(int valueToBeInserted)

See example below:

class  MyClass
{
 private:
 int sizeOfMyArray;
 int posArrayIterator;
 int negArrayIterator;
 int myNarray[20];
 int myPArray[20];    

 public:
 MyClass(){
     sizeOfMyArray = 20;
     posArrayIterator = 0;
     negArrayIterator = 0;
  };

 bool addInt(int value); 
 void printNArray()
 {
   cout << "[ ";
   for (int i=0; i<negArrayIterator; i++)
   {
     cout << myNarray[i];
     if ( (i+1) < negArrayIterator ) 
     {
         cout << ", "; 
     }
   }
   cout << "]";
 }
};

 bool MyClass::addInt(int valueToBeInserted){

 if (valueToBeInserted >= 0){
    if (posArrayIterator >= sizeOfMyArray){
      return false;
    } else {
      cout << "added " <<  valueToBeInserted << "\n" << endl;
      myPArray[posArrayIterator] = valueToBeInserted;
      posArrayIterator ++;
      return true;
    } 
 } else {
        if (negArrayIterator >= sizeOfMyArray){
            return false;
        }
        else {
           cout << "added " <<  valueToBeInserted << "\n" << endl;
           myNarray[negArrayIterator] = valueToBeInserted;
           negArrayIterator ++;
           return true;
      }
 }
}

int main()
 {
  MyClass b;
  b.addInt(-1);
  b.addInt(-3);
  b.addInt(-9);
  b.printNArray();
  return 0;
 }

Output:

added -1

added -3

added -9

[ -1, -3, -9]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM