Reputation: 21
i have a pretty weird problem. what im doing is im trying to convert a 8-digit binary number in a string to a decimal number (string also) at the end of the code i cout the binary string and the decimal string but when i ran, i only successfully see the binary string but not the decimal string...
heres my code:
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
void bintodec(string bin);
int main()
{
string bin="";
bintodec(bin);
return 0;
}
void bintodec(string bin)
{
int temp;
int temp_a;
int temp_b;
string dec;
cout << "Enter the binary number: ";
cin >> bin;
temp = 128*(bin[0]-48) + 64*(bin[1]-48) + 32*(bin[2]-48) + 16*(bin[3]-48) + 8*(bin[4]-48) + 4*(bin[5]-48) + 2*(bin[6]-48) + (bin[7]-48);
temp_a = temp%10;
temp = (temp - temp_a) / 10;
temp_b = temp%10;
temp = (temp - temp_b) / 10;
dec[2]=temp_a+48;
dec[1]=temp_b+48;
dec[0]=temp+48;
dec[3]='\0';
cout << endl << bin << " in decimal is: " << dec << endl;
}
and here is the running result:
Enter the binary number: 10101010
10101010 in decimal is:
after "is" there should be my decimal number; however theres nothing. i tried to cout dec[0] dec[1] and dec[2] individually and it worked, but when i cout the whole dec, i failed every time...
could anyone tell me where my problem is? i think i have something wrong with my code but i could figure it out...
Upvotes: 2
Views: 184
Reputation: 3707
The size of dec
is zero. But you access elements at positions 0 to 3.
You could for example create dec
initialized to the appropriate size by using
string dec(4, ' '); // fill constructor, creates a string consisting of 4 spaces
instead of
string dec;
.
Upvotes: 4
Reputation: 24249
Note: This is simply a comment-with-code and not an actual answer.
int main()
{
string bin="";
decode(bin);
}
void decode(string bin)
{
}
This causes a new string to be created on entry into "decode" and the contents of "bin" copied to it. Any changes made to decode::bin will be invisible to main when decode ends.
You can avoid this - called "pass by value" because you are passing the values of "bin" and not "bin" itself - by using "pass by reference"
void decode(string& bin)
But in your code, you don't actually appear to need to pass "bin" at all. If you are going to need "bin" in main after decode, you might instead consider returning it:
int main()
{
string bin = decode();
}
string decode()
{
string bin = "";
...
return bin;
}
But for now, just remove bin from main and make it a local variable in decode.
void bintodec();
int main()
{
bintodec();
return 0;
}
void bintodec()
{
std::string bin = "";
cout << "Enter the binary number: ";
cin >> bin;
int temp = 128*(bin[0]-48) + 64*(bin[1]-48) + 32*(bin[2]-48) + 16*(bin[3]-48) + 8*(bin[4]-48) + 4*(bin[5]-48) + 2*(bin[6]-48) + (bin[7]-48);
int temp_a = temp%10;
temp = (temp - temp_a) / 10;
int temp_b = temp%10;
temp = (temp - temp_b) / 10;
char dec[4] = "";
dec[2] = temp_a+48;
dec[1] = temp_b+48;
dec[0] = temp+48;
dec[3] = '\0';
cout << endl << bin << " in decimal is: " << dec << endl;
}
Upvotes: 0
Reputation: 18652
@SebastianK already addressed the fact that your std::string
had a zero length. I wanted to add that instead of the following:
dec[2]=temp_a+48;
dec[1]=temp_b+48;
dec[0]=temp+48;
dec[3]='\0';
You can append characters to the empty std::string
using the push_back()
member function:
dec.push_back(temp + '0');
dec.push_back(temp_b + '0');
dec.push_back(temp_a + '0');
Note that you don't need to NULL terminate a std::string
and I used a character literal '0'
instead of the ASCII value 48 because I think it's clearer.
Upvotes: 1