-
April 23rd, 2013, 11:06 PM
#1
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;
}
-
April 23rd, 2013, 11:30 PM
#2
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;
}
-
April 23rd, 2013, 11:39 PM
#3
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.
-
April 24th, 2013, 05:17 AM
#4
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?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|