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

+7 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 <iostream>
#include <string.h>
#include <cstdlib>

using namespace std;

int main(){
string cmd;

long long ax = 0;
bool error = false;

cout << "Type in command (type \"HELP\" to see all commands): " <<endl;
while(1){
error = false;
cout << ">";
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;
}
}
}

Post a Reply

Replies

- page 5
Oldest  Newest  Rating
0 Ed Gon · December 4, 2014
@wheately

you just change the interface, its just my code. -.-
0 Ed Gon · December 4, 2014
@wheately aw so sorry appreciated the effort!
-1 Ed Gon · December 10, 2014
@franz i try manipulating the code that you provided. i met some issues with it.

#include <iostream>
#include <string.h>
#include <cstdlib>
#include <math.h>
#include <string>

using namespace std;

long long AX = 0;
long long OP1 = 0;
long long OP2 = 0;
enum CMDS {ADD, PRT, EXT, ERR} OP_CODE;//enum is a variable type which can take different fits
bool EXIT = false;

void init(){
//OP_CODE = ERR;
cout << "Type in this format ADD (x,y): "  << 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)){
OP_CODE = 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
//OP1 = atoi(x.c_str());
}
else{
y = cmd.substr(cmd.find(",") + 1);//store the value of y
                //OP2 = atoi(y.c_str());
}
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{
OP_CODE = ERR;
}
}
else if (cmd.find("PRT") == 0 && cmd.length() == strlen("PRT")){//Triggers the PRT AX
OP_CODE = PRT;
        }
}
else if (cmd.find("EXT") == 0 && cmd.length() == strlen("EXT")){//Triggers the EXIT
OP_CODE = EXT;
        }
else{
OP_CODE = ERR;
}
}
void execute(){
switch (OP_CODE){
        case ADD:
            AX = OP1 + OP2;
            break;
case PRT:
cout << ">AX = " << AX << endl;//Prints the result
break;
case EXT:
EXIT = true;
break;
case ERR:
cout << ">Error: Unknown command!" << endl;
break;
}
}
int main(){
string cmd;
init();
do{
cmd = getinput();
decode(cmd);
execute();
} while (!EXIT);
}


http://i.imgur.com/VD4LeS4.jpg

C++

129,960 followers
About

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

Links
Moderators
Bucky Roberts Administrator