The strcpy(0 function copies entire c-string to memory position you designated as destination. In code
char tmp2[5];
strcpy(tmp2, tmp.c_str());
In code
binarycode[0] = tmp2;
you attempted to save pointer - address of that buffer to a byte.
you statically allocated 5 bytes(!) of memory, then attempted to copy string to that memory. If anything, you would cause memory corruption this way, because rest of string would go somewhere.
C++ is not Java and you should thoroughly read books on this language, about syntax and standard you're using, not relying on something that "looks like". There are even principal differences between C and C++ in some areas.
If anything, iostreams provide all tools you need to get values from user input, but "proper" way to do requires handling cases of incorrect input. Consider this function:
#include <limits>
#include <iostream>
char getChar()
{
while (1) // Loop until user enters a valid input
{
std::cout << "Enter a byte value: ";
int x; // if we'll use char, cin would assume it is a character
std::cin >> x;
if (std::cin.fail()) // has a previous extraction failed?
{
// let's handle the failure
// or next >> will try parse same input
std::cout << "Invalid input from user.\n";
std::cin.clear(); // put us back in 'normal' operation mode
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
// and remove the bad input
}
//else if ( ((char)(x & 0xff)) != x ){
else if(( x > std::numeric_limits<char>::max()) ||
( x < std::numeric_limits<char>::min()))
{
// char can be from -127 to 127, it's one byte..
// int is allowing large values
std::cout << "Invalid value.\n";
}
else // nope, so return our good x
return (char)(x & 0xff);
}
}
The char is a pain in the backside with std::iostream, it always should be an int instead. Casting to smaller variable, like (char)x might be an undefined behavior, so need to mask larger values (char)(x & 0xff); For other types this function can become a template based on the type requested.
Now we should make it understand binary input? there is no predefined manipulator for binary format, you must input a string, validate it, and convert yourself.
int binaryToDec(std::string number)
{
int result = 0, pow = 1;
for ( int i = number.length() - 1; i >= 0; --i, pow <<= 1 )
result += (number[i] - '0') * pow;
return result;
}
std::string validateEntry()
{
bool valid = true;
std::string tempStr;
do
{
valid = true;
getline(std::cin, tempStr);
for (int i = 0; i < tempStr.length(); i++)
{
if ((tempStr.compare(i, 1, "0") != 0) && (tempStr.compare(i, 1, "1") != 0))
{
valid = false;
std::cout << "Enter Valid Binary Number: ";
break;
}
}
} while (valid == false);
return tempStr;
}
Use those in pattern:
std::cout << "Enter Binary Number: ";
std::string binaryString = validateEntry();
int binaryNum = binaryToDec(binaryString);
strcpyin code like this is discouraging. If you're doing C++, please, please usestd::string. Thestrcpyfunction is notoriously problematic. In this short example you have a gigantic buffer-overflow bug which is why it's really a bad idea to even start down this road.