binary to decimal with function

hello,what is the problem in this program?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "stdafx.h"
#include<iostream>
#include<cmath>

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<<sum;
	

}

int checkbinary(int number)
{
int remain,i,counter;
double power,sum=0;
const int arreysize=1000;
	int binarrey[arreysize];


		for(i=0;number!=0;i++)
		{
		number=number/10;
		remain=number%10;
		
		if(remain>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 whybut 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// check 4.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<cmath>

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<<checkbinary(number);
	
	

}






int checkbinary(int number)
{
int remain,i,counter;
double power,sum=0;
const int arreysize=1000;
	int binarrey[arreysize];


		for(i=0;number!=0;i++)
		{
		remain=number%10;
		number=number/10;
	
		
		if(remain>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;

}




//*********************************************



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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <string>
#include <iostream>

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <math.h>
#include <string.h>

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:
1
2
3
4
// C++ String
#include <string>

std::string myString = "Hello, World!";


1
2
3
// 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:
1
2
3
4
5
6
7
8
9
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.