Adding and Subtracting integer arrays
Ok so I am currently working on a program that uses a class to store integer arrays. I have most of the code done, but I am having trouble on the main.cpp part of my program. The program should display this:
OBJ 1:
2 3 4
OBJ 2:
1 4 -2
Addition:
OBJ 1 =
3 7 6
Subtraction:
OBJ 1 = 1 -1 6
But when I run and compile my program, this is what I get:
OBJ 1:
2 3 4
OBJ 2:
ADDING:
OBJ 1 =
4 6 8
Subtraction:
OBJ 1 =
I would greatly appreciate it if someone could help me with this. Here is my current main.cpp:
Code:
#include <iostream>
#include "SafeArray.h"
using namespace std;
int main()
{
// Declare a SafeArray object
Safe obj;
Safe obj2;
int x;
obj.set(2,0);
obj.set(3,1);
obj.set(4,2);
cout << "OBJ 1: " << endl;
obj.print();
cout << "OBJ 2: " << endl;
obj2.print();
cout << "ADDING: " << endl;
obj.add(obj2);
cout << "OBJ 1 = " << endl;
obj.print();
cout << " Subtraction: " << endl;
obj.subtract(obj2);
cout << " OBJ 1 = " << endl;
obj.print();
return 0;
}
Re: Adding and Subtracting integer arrays
Ok so I figured out the problem, but now my program will not subtract properly. Can anybody here take a look at my program and help me figure this out?
Code:
#include <iostream>
#include "SafeArray.h"
using namespace std;
int main()
{
// Declare a SafeArray object
Safe obj;
Safe obj2;
int x;
obj.set(5,0);
obj.set(6,1);
obj.set(7,2);
obj2.set(4,0);
obj2.set(3,1);
obj2.set(2,2);
cout << "OBJ 1: " << endl;
obj.print();
cout << "OBJ 2: " << endl;
obj2.print();
cout << "ADDING: " << endl;
obj.add(obj2);
cout << "OBJ 1 = " << endl;
obj.print();
cout << " Subtraction: " << endl;
obj.subtract(obj2);
cout << "OBJ 1 = " << endl;
obj.print();
return 0;
}
Re: Adding and Subtracting integer arrays
This Safe class is not part of the C++ standard library, so I am afraid that I have no clue as to how it works. Based on what you wrote, it seems that you implemented it, so you definitely have a bug in your code.
Re: Adding and Subtracting integer arrays
As the problem will be in the Safe class and you haven't provided the source we can't really advise other than to re-iterate what laserlight said in post #3 that you have a bug. Have you used the debugger to trace through the coe to find the cause of the problem?