Hello Hasnain Attarwala,
When I loaded your code the first problem I had came from the opening comment. It may have been a copy problem that you missed the opening "/*" of the comment, but it gave the compiler fits when I tried to compile the program.
Something that may help is to write your comment as:
1 2 3 4
|
/*
FUNCTION: This program adds and subtracts binary with some flags
********************************************************************
*/
|
Putting the open and closing "/* */" on their own line helps to see what is to be commented.
After working with the program a bit I changed some parts:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include <iostream> // io to screen
#include <iomanip> // <--- Works with "iostream" to manipulate the output. Usually "setw()" and "setprecision".
#include <string>
#include <fstream> //io files
//#include <cstring> //strlen function
//#include <sstream>
//using namespace std; // <--- Best not to use.
// A recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/
// Add your function prototypes here
std::string isEven(std::string v);
std::string findTwoscomplement(std::string str);
std::string fullAdder(const std::string&, const std::string&, std::string&, std::string&);
|
The first group of header I have found that these tend to be the most used in any program. I also found that alphabetical order helps you to remember when you forget a header file. Since "iomanip" works with "iostream" I generally keep them in this order.
The second group of header files would be any header file that the program needs.
The third group is there to show you the header files that you do not need or use in this program.
The comments on
using namespace std;
should explain that part.
In the prototype for "fullAdder" I changed this. First I passed the first two variables by reference. The advantage here is that you can see the whole string in the function when debugging and second it is easier to pass by reference rather than having to make a copy for the function. Lastly I made the variables "const" so they could not be changed in the function.
When defining variable names try not to use single letter names. This makes the program hard to follow. Since this is a function you could use the variable names "v1" and "v2" because they are local to the function and will not conflict with what you defined in "main" because the scope of the program has changed.
This is just the beginning of the function to show what I changed:
1 2 3 4 5 6 7 8 9
|
//full adder function
std::string fullAdder(const std::string& v1, const std::string& v2, std::string& inPtr, std::string& outPtr)
{
size_t len1 = v1.length(), len2 = v2.size();
size_t maxSize = std::max(len1, len2); // <--- Requires "algorithm" header file.
std::string result(maxSize + 1,'0'); // <--- Initialize result
int s = 0; // Initialize digit sum
for (size_t i = maxSize - 1; i >= 0; i--)
|
The function parameters show how I changed the first two.
Line 4 shows that "length" and "size" functions return the same value. Eventually I came to using "size" because it returns the same value as "length" and it is shortrt to type.
Line 5 will return the larger of the two "len?" variables or if they are the same size it will return the correct value.
Lines 4 and 5 are just thinking ahead in that "v1" or "v2" may be different lengths. If your program needs "v1" and "v2" to be the same length then you need to check for this back in "main" just after you read the file.
The functions ".length()" and ".size()" both return a "size_t" variable which is usually a typedef for an "unsigned int", but not always the case. You should check your compiler and header file to see how this is defined. Not knowing what IDE you are using I am not sure where to tell where to look. Foe me and my VS2017 IDE it turned out to be in the "vcruntime.h" header file.
The next problem I had was with
std::string result;
. Defining it this way just creates an empty container with no size. Later on in the code you have
result[i] = ('0');
. First I do not believe the () around "0" are needed. More importantly is
result[i]
. If "i" is 3 then it would be
result[3]
, but "result" is empty at this point, so you are trying to write to a memory location that does not exist yet. Line 6 takes care of this problem by initializing the string to "maxSize" and setting each element to '0'. The "+ 1" is because the result could be one larger than you original string.
After that there are problems with the code that produce wrong answers. Given the input strings of "00" and "11" the result should be "11", but it is coming out as "00" which is wrong. Then there is the second two numbers although the string sizes are equal in length the result of addition would be one character larger than the original string length. This part I need to do some more work on.
I hope that helps for now,
Andy