# Help me for the insertion of binary function in my add function

 Ed Gon · December 3, 2014 Here is my code, I need the to insert the process where the inputed number converts to binary digit and those numbers will be add in binary form then converts back to decimal as a result. PLEASE HELP~=======#include #include #include using namespace std;int main(){ string cmd; long long ax = 0; bool error = false; cout << "Type in command (type \"HELP\" to see all commands): " <"; getline(cin, cmd);//catches the data if (cmd.find("ADD") == 0){ if (cmd.find(" ") == strlen("ADD")){ string x = cmd.substr(strlen("ADD "), cmd.length()-(cmd.length() - cmd.find(",") + strlen("ADD ")));//store the value of x string y = cmd.substr(cmd.find(",") + 1);//store the value of y ax = atoi(x.c_str()) + atoi(y.c_str()); //calculates the result } else{ error = true; } } else if (cmd.find("PRT AX") == 0 && cmd.length() == strlen("PRT AX")){//Triggers the PRT AX cout << ">AX = " << ax << endl;//Prints the result } else if (cmd.find("HELP") == 0 && cmd.length() == strlen("HELP")){//Triggers the HELP cout << ">" << endl; cout << ">ADD x,y add x and y and save the result in AX" << endl; cout << ">PRT AX display the value of AX" << endl; cout << ">EXIT exit the application" << endl; cout << ">" << endl; } else if (cmd.find("EXIT") == 0 && cmd.length() == strlen("EXIT")){//Triggers the EXIT break; } else{ error = true; } if (error){ cout << ">Error: Unknown command!" << endl; } }}

## Replies

- page 4

 Franz Schmidt · December 7, 2014 And the modification isn't that hard you only need to split the main into 3 functions and add those 3 variables. Franz Schmidt · December 7, 2014 So this is your FINAL INSTRUCTION ``#include #include using namespace std;long long AX = 0;long long OP1 = 0;long long OP2 = 0;enum CMDS {ADD, PRT, EXT, HELP, ERR} OP_CODE;//enum is a variable type which can take different fitsbool EXIT = false;void init(){ OP_CODE = ERR; cout << "Type in command (type \"HELP\" to see all commands): " << endl;}string getinput(){ string input; cout << ">" << endl << ">"; getline(cin, input);//catches the data return input;}void decode(string cmd){ if (cmd.find("ADD") == 0){ if (cmd.find(" ") == strlen("ADD")){ OP_CODE = ADD; string x = cmd.substr(strlen("ADD "), cmd.length() - (cmd.length() - cmd.find(",") + strlen("ADD ")));//store the value of x string y = cmd.substr(cmd.find(",") + 1);//store the value of y OP1 = atoi(x.c_str()); OP2 = atoi(y.c_str()); //calculates the result } else{ OP_CODE = ERR; } } else if (cmd.find("PRT AX") == 0 && cmd.length() == strlen("PRT AX")){//Triggers the PRT AX OP_CODE = PRT; } else if (cmd.find("HELP") == 0 && cmd.length() == strlen("HELP")){//Triggers the HELP OP_CODE = HELP; } else if (cmd.find("EXIT") == 0 && cmd.length() == strlen("EXIT")){//Triggers the EXIT OP_CODE = EXT; } else{ OP_CODE = ERR; }}void execute(){ switch (OP_CODE){ case ADD: //convert x and y to binary and save binary values in b_x and b_y bool b_x[31]; bool b_y[31]; long long x_num; long long y_num; x_num = OP1; y_num = OP2; for (int n = 30; n >= 0; n--){ if ((x_num - pow(2, n)) >= 0){ b_x[n] = 1; x_num -= pow(2, n); } else if ((x_num + pow(2, n)) <= 0){ b_x[n] = 1; x_num += pow(2, n); } else{ b_x[n] = 0; } if ((y_num - pow(2, n)) >= 0){ b_y[n] = 1; y_num -= pow(2, n); } else if ((y_num + pow(2, n)) <= 0){ b_y[n] = 1; y_num += pow(2, n); } else{ b_y[n] = 0; } } /*cout << endl << "Direct conversion: "; cout << endl << "b_x: "; for (int n = 30; n >= 0; n--){ cout << b_x[n]; } cout << endl << "b_y: "; for (int n = 30; n >= 0; n--){ cout << b_y[n]; }*/ //add/substract numbers in binary form bool b_sum[31]; bool carry; x_num = OP1; y_num = OP2; carry = 0; if (x_num >= 0 && y_num >= 0 || x_num <= 0 && y_num <= 0){ //addition for (int n = 0; n < 31; n++){ if (carry){ if (b_x[n] && b_y[n]){ carry = 1; b_sum[n] = 1; } else{ carry = 0; if (b_x[n] || b_y[n]){ carry = 1; b_sum[n] = 0; } else{ b_sum[n] = 1; } } } else{ if (b_x[n] && b_y[n]){ carry = 1; b_sum[n] = 0; } else{ b_sum[n] = b_x[n] | b_y[n]; } } } } else{ //substraction bool b_big[31]; bool b_small[31]; bool borrow[31] = { 0 };//variable for substraction if (x_num < 0){ x_num *= (-1); } if (y_num < 0){ y_num *= (-1); } if (x_num > y_num){ memcpy(b_big, b_x, sizeof(b_x)); memcpy(b_small, b_y, sizeof(b_y)); } else{ memcpy(b_big, b_y, sizeof(b_y)); memcpy(b_small, b_x, sizeof(b_x)); } for (int n = 0; n < 31; n++){ if (b_small[n]){ if (!b_big[n] && !borrow[n]){ int u = 1; while (1){ if (b_big[n + u]){ b_big[n + u] = 0; break; } else{ borrow[n + u] = 1; } u++; } b_sum[n] = 1; } else{ if (b_big[n] && borrow[n]){ b_sum[n] = 1; } else{ b_sum[n] = 0; } } } else if (b_big[n]){ if (borrow[n]){ b_sum[n] = 0; } else{ b_sum[n] = 1; } } else{ if (borrow[n]){ b_sum[n] = 1; } else{ b_sum[n] = 0; } } } } /*cout << endl << "sum: "; for (int n = 30; n >= 0; n--){ cout << b_sum[n]; } cout << endl;*/ //convert binary sum back to a normal dezimal number long long sum; x_num = OP1; y_num = OP2; sum = 0; for (int n = 30; n >= 0; n--){ if (b_sum[n] == 1){ sum += pow(2, n); } } if ((x_num <= 0 && y_num <= 0) || (x_num < 0 && (x_num * (-1)) > y_num) || (y_num < 0 && (y_num * (-1)) > x_num)){ sum *= (-1); } /*cout << "Sum from binary addition: " << sum << endl;*/ AX = sum; break; case PRT: cout << ">AX = " << AX << endl;//Prints the result break; case EXT: EXIT = true; case HELP: cout << ">ADD x,y add x and y and save the result in AX" << endl; cout << ">PRT AX display the value of AX" << endl; cout << ">EXIT exit the application" << endl; break; case ERR: cout << ">Error: Unknown command!" << endl; break; }}int main(){ string cmd; init(); do{ cmd = getinput(); decode(cmd); execute(); } while (!EXIT);}``Hope you understand everything.If something is not clear just ask Ed Gon · December 7, 2014 WOW DUDE! I'm totally impressed. I hope reach can your programming skills, I'm skills still sucks but my analysis is keep me hanging on. anyway I'm a bit lost in this part  decode("ADD 23, 34"); Is this an executable command?  as what the instructions says.Create the following global variables:OPCODE which will hold the current operation. In the above particular case, it is ADD.OP1 which will hold the first parameter which is 23. This will become optional later when additional operations are included.OP2 which will hold the second parameter, which is 34. This will become optional later when additional operations are included.and I found some issue if I input "ADD x,4" the numeric still stores in "y"  and still printed if "PRT AX" is trigger "AX = 4" Franz Schmidt · December 7, 2014 decode("ADD 23,34"); is NOT a function that you can enter as input.It is the internal function (hope you know how to handle functions in C/C++) which splits the string into the varialbes OPCODE, OP1 and OP2 and this function only is used for this (not for the addition or substraction). Short it just decodes the string into variables so you can handle the instructions easier.What do you don't understand at the second part?And yes this is because atoi() only takes REAL num_chars (from 0 to 9) as valid and the rest is equal to 0Can see that if you execute binary variables (so x = 0) Franz Schmidt · December 7, 2014 I made some features in the code, but it got a little messy in the decode function But now every conditions should be fulfilled.``#include #include using namespace std;long long AX = 0;long long OP1 = 0;long long OP2 = 0;enum CMDS { ADD, PRT, EXT, HELP, ERR } OPCODE;bool EXIT = false;enum ERRORS { CMD, NUM, SYNTAX} ERR_MSG;void init(){ OPCODE = ERR; cout << "Type in command (type \"HELP\" to see all commands): " << endl;}string getinput(){ string input; cout << ">" << endl << ">"; getline(cin, input);//catches the data return input;}void decode(string cmd){ if (cmd.find("ADD") == 0){ if (cmd.find(" ") == strlen("ADD") && cmd.find(",") != strlen("ADD ") && cmd.find(",") != string::npos && cmd.find(",", cmd.find(",") + 1) == string::npos && cmd.find(" ", cmd.find(" ") + 1) != (cmd.find(" ") + 1) && cmd.find(" ", cmd.find(" ") + 1) != cmd.find(" ", cmd.find(" ") + 1)){ OPCODE = ADD; string x; string y; x = cmd.substr(strlen("ADD "), cmd.length() - (cmd.length() - cmd.find(",") + strlen("ADD ")));//store the value of x if (cmd.find(" ", cmd.find(","), 1) == cmd.find(",") + 1){ y = cmd.substr(cmd.find(" ", cmd.find(",")) + 1);//store the value of y } else{ y = cmd.substr(cmd.find(",") + 1);//store the value of y } if (strspn(x.c_str(), "0123456789") && strspn(y.c_str(), "0123456789")){ OP1 = atoi(x.c_str()); OP2 = atoi(y.c_str()); //calculates the result } else{ OPCODE = ERR; ERR_MSG = NUM; } } else{ OPCODE = ERR; ERR_MSG = SYNTAX; } } else if (cmd.find("PRT AX") == 0 && cmd.length() == strlen("PRT AX")){//Triggers the PRT AX OPCODE = PRT; } else if (cmd.find("HELP") == 0 && cmd.length() == strlen("HELP")){//Triggers the HELP OPCODE = HELP; } else if (cmd.find("EXIT") == 0 && cmd.length() == strlen("EXIT")){//Triggers the EXIT OPCODE = EXT; } else{ OPCODE = ERR; ERR_MSG = CMD; }}void execute(){ switch (OPCODE){ case ADD: //convert x and y to binary and save binary values in b_x and b_y bool b_x[31]; bool b_y[31]; long long x_num; long long y_num; x_num = OP1; y_num = OP2; for (int n = 30; n >= 0; n--){ if ((x_num - pow(2, n)) >= 0){ b_x[n] = 1; x_num -= pow(2, n); } else if ((x_num + pow(2, n)) <= 0){ b_x[n] = 1; x_num += pow(2, n); } else{ b_x[n] = 0; } if ((y_num - pow(2, n)) >= 0){ b_y[n] = 1; y_num -= pow(2, n); } else if ((y_num + pow(2, n)) <= 0){ b_y[n] = 1; y_num += pow(2, n); } else{ b_y[n] = 0; } } /*cout << endl << "Direct conversion: "; cout << endl << "b_x: "; for (int n = 30; n >= 0; n--){ cout << b_x[n]; } cout << endl << "b_y: "; for (int n = 30; n >= 0; n--){ cout << b_y[n]; }*/ //add/substract numbers in binary form bool b_sum[31]; bool carry; x_num = OP1; y_num = OP2; carry = 0; if (x_num >= 0 && y_num >= 0 || x_num <= 0 && y_num <= 0){ //addition for (int n = 0; n < 31; n++){ if (carry){ if (b_x[n] && b_y[n]){ carry = 1; b_sum[n] = 1; } else{ carry = 0; if (b_x[n] || b_y[n]){ carry = 1; b_sum[n] = 0; } else{ b_sum[n] = 1; } } } else{ if (b_x[n] && b_y[n]){ carry = 1; b_sum[n] = 0; } else{ b_sum[n] = b_x[n] | b_y[n]; } } } } else{ //substraction bool b_big[31]; bool b_small[31]; bool borrow[31] = { 0 };//variable for substraction if (x_num < 0){ x_num *= (-1); } if (y_num < 0){ y_num *= (-1); } if (x_num > y_num){ memcpy(b_big, b_x, sizeof(b_x)); memcpy(b_small, b_y, sizeof(b_y)); } else{ memcpy(b_big, b_y, sizeof(b_y)); memcpy(b_small, b_x, sizeof(b_x)); } for (int n = 0; n < 31; n++){ if (b_small[n]){ if (!b_big[n] && !borrow[n]){ int u = 1; while (1){ if (b_big[n + u]){ b_big[n + u] = 0; break; } else{ borrow[n + u] = 1; } u++; } b_sum[n] = 1; } else{ if (b_big[n] && borrow[n]){ b_sum[n] = 1; } else{ b_sum[n] = 0; } } } else if (b_big[n]){ if (borrow[n]){ b_sum[n] = 0; } else{ b_sum[n] = 1; } } else{ if (borrow[n]){ b_sum[n] = 1; } else{ b_sum[n] = 0; } } } } /*cout << endl << "sum: "; for (int n = 30; n >= 0; n--){ cout << b_sum[n]; } cout << endl;*/ //convert binary sum back to a normal dezimal number long long sum; x_num = OP1; y_num = OP2; sum = 0; for (int n = 30; n >= 0; n--){ if (b_sum[n] == 1){ sum += pow(2, n); } } if ((x_num <= 0 && y_num <= 0) || (x_num < 0 && (x_num * (-1)) > y_num) || (y_num < 0 && (y_num * (-1)) > x_num)){ sum *= (-1); } //cout << "Sum from binary addition: " << sum << endl; AX = sum; break; case PRT: cout << ">AX = " << AX << endl;//Prints the result break; case EXT: EXIT = true; case HELP: cout << ">ADD x,y add x and y and save the result in AX" << endl; cout << ">PRT AX display the value of AX" << endl; cout << ">EXIT exit the application" << endl; break; case ERR: cout << ">Error: "; switch (ERR_MSG){ case CMD: cout << "Unknown command!"; break; case NUM: cout << "Enter only numbers!"; break; case SYNTAX: cout << "Take care of the input syntax!"; break; } cout << endl; break; }}int main(){ string cmd; init(); do{ cmd = getinput(); decode(cmd); execute(); } while (!EXIT);}`` Ed Gon · December 7, 2014 Okay, i get it now. Thanks a lot. Any tips on how to improve my programming skills? Franz Schmidt · December 7, 2014 Just practice Adeline Faye Lompon · December 16, 2014 What is itoa means in the code ? itoa(BinToDec(AddBin(OP1, OP2)), str, 10); Ed Gon · December 4, 2014 @Franzmy code does:if I input ADD 4,3 the 4,3 is been add 4+3 but its not been shown yet unless you input PRT AX.thus my problem now is to convert those decimal digits into binary which is the 0100 (4) and 0011 (3).then add those binary digits in binary form then the result must be converted back to decimal form. if I input PRT AX the decimal form should appear.I hope you can help Ed Gon · December 4, 2014 @krootushas gesuyea i know, but I am making a virtual machine(for my course project,so yea) on how computer understands. which its a hassle.

## C++

129,771 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
 Bucky Roberts Administrator