### binary to decimal with function

hello,what is the problem in this program?
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768`` ``````#include "stdafx.h" #include #include using namespace std; void BinarytoDecimal(); int checkbinary(int ); int _tmain(int argc, _TCHAR* argv[]) { BinarytoDecimal(); } void BinarytoDecimal() { const int arreysize=1000; int binarrey[arreysize]; int number,sum; static int i=0; cout<<"Enter the binary number:"; cin>>number; checkbinary(number); cout<1) { cout<<"the number is wrong.please try again!"; BinarytoDecimal(); } else binarrey[i]=remain; } for(counter=0;counter<=i;counter++) { power=counter; sum=pow(2,power)*binarrey[counter]+sum; } return sum; }``````
At line 33 `cout<<sum;` the variable is output but it was never initialised, so the output is unpredictable.

At line 66 `return sum;` a value is returned from the function, but when the function is called on line 31 nothing is done with this result.
Last edited on
ok I know that. but why`but when the function is called on line 31 it does not do anything with this result.`
Last edited on
Lets say you want to find the square root of 25.

`sqrt(25.0);`
will do that.

But if you want to make use of the result you either need to display it directly
` cout << sqrt(25.0);`

or assign the result to a variable
` double x = sqrt(25.0);`

The same applies to the function you wrote yourself.
thank you so much...
I solved that problem but I think that this program has many problem yet.
thank you anyway
How can I get very long number in this program?
for example:0111100000000000011111111111101
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091`` ``````// check 4.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include using namespace std; void BinarytoDecimal(); int checkbinary(int ); int _tmain(int argc, _TCHAR* argv[]) { BinarytoDecimal(); } void BinarytoDecimal() { long int number,sum; static int i=0; cout<<"Enter the binary number:"; cin>>number; cout<1) { cout<<"the number is wrong.please try again!"; BinarytoDecimal(); } else binarrey[i]=remain; } for(counter=0;counter
Last edited on
I would make `number` a string instead of a long int.
ok,I didn't can use this.
can you explain more?
in checkbinary function it makes problem and I don't know what to do!!!!!!!!!!!

what aboat arrey?
What shoud I do if I want solve it with arrey?
Last edited on
 ``12345678910111213141516171819202122`` ``````#include #include int BinaryToDecimal(std::string& in) { int decimal = 0; while (!in.empty()) { decimal *= 2; if (in[0] == '1') decimal += 1; in.erase(in.begin()); } } int main() { std::string input; std::cout << "Input a binary number: "; std::cin >> input; }``````
thank you so much...
If I just want to use arrey what shoud I do?(I shoudn't use string)
who knows?????
You can get this example to know how to implement this with string:
 ``1234567891011121314151617181920212223242526272829303132`` ``````#include #include #include using namespace std; long int BinaryToDecimal (string binary) { long int valor=0; int a; for (int i=0; i <= binary.size()-1; i++) { a = binary[(binary.size()-1)-i]; //cout << a-48 << endl; valor += pow(2, i) * (a-48); // -48, because the return is 1 on the ASCII table, and the first number 0 is 48 } return valor; } int main () { string binary; cout << "Inform binary: "; cin >> binary; cout << endl; cout << " Result : " << BinaryToDecimal(binary) << endl; return 0; } ``````
thank you.
but "without using string"!!!!!!!!!!!
You need to specify since there are two styles of strings. You still need to use a string, but you want a C-String instead of a C++-String. The difference:
 ``1234`` ``````// C++ String #include std::string myString = "Hello, World!";``````

 ``123`` ``````// C String char* myString[]= "Hello, World!";``````

For all intensive purposes, they're the same. If you need an array of C-Strings, you can do something like:
 ``123456789`` ``````char* myStringArray[10][10]; // Allows 10 strings with a max length of 10 chars // Reads 10 strings from the user for (int i = 0; i < 10; i ++) std::cin >> myStringArray[i]; // Displays 10 strings for (int i = 0; i < 10; i ++) std::cout << myStringArray[i] << "\n";``````
Topic archived. No new replies allowed.