dec to bin

Pages: 12
please help me!!!!!!
how can I fix this program?this program can't get very long numbers(because of "int")
for example:111111100011111101111

I shoudn't use string.
I just shoud use arrey.

thank you.





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

}


Why don't you use double instead of int?
It can't resolve my problem!
You can use cin.get or cin.getline to obtain the input in a character array.
http://www.cplusplus.com/reference/istream/istream/getline/
http://www.cplusplus.com/reference/istream/istream/get/
can you explain more?
I really beginner!!!!!!
I know what is cin.get but I don't know where do I should use it!
(Excuse me for my bad ENGLISH!!!)
Please look at the example code in the pages I linked. That shows how to use it.

After that you can loop through the array until you reach the terminating null character '\0';
Last edited on
chervil!
I don't understand how use it!!!!!!!!
Which part don't you understand?
There are three stages
1) get the input from the user, into the array
2) loop through the array one character at a time
3) use each character in a very similar manner to the code posted previously by StewBond http://www.cplusplus.com/forum/beginner/88122/#msg472669
Slightly simpler actually, as you don't need the erase() in this case.
Last edited on
get the input from the user, into the array
These lines copied from one of the examples:

1
2
3
4
  char str[256];

  cout << "Enter the name of an existing text file: ";
  cin.get (str,256);



Here's one of the other examples
1
2
3
4
  char name[256];

  cout << "Enter your name: ";
  cin.getline (name,256);


The figure 256 is entirely your choice, as to how many digits you want to allow the user to input.
thank you .You can think I understood it!!!!
when I use cin.get,how can I use characters for cinverting bin to dec?
They should be integer.
who can help me???????!!!!!!!!!!!
for example in this code ascii codes have been used not value of'0' or '1'
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
#include "stdafx.h"
#include<iostream>
#include<cmath>


using namespace std;



int _tmain(int argc, _TCHAR* argv[])
{
	int i=0,counter;
	const int size=1000;
	int binarrey[size];
		double power,sum=0;
		int num;
	cout<<"Enter the binarey number";

	while((num=cin.get())!='\n')
	{
	
	
			binarrey[i]=num;
			i++;
		}
			i--;
			;

	for(power=0,counter=i;power<=i;power++,counter--)
	sum=pow(2,power)*binarrey[counter]+sum;
	cout<<sum;

}
oh!my friends!
Where are you?
please answer my question...
Last edited on
closed account (18hRX9L8)
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
/*Created by Usandfriends */
#include <iostream>
using namespace std;

main()
{
      int bin0,bin1,bin2,bin3,bin4,bin5,bin6,bin7,tot;
      
      begin:
      printf ("Binary to Decimal.\n");
      printf ("Enter binary:\n");
      printf ("1st Char (2^0):  ");
      cin>>bin7;
      printf ("2nd Char (2^1):  ");
      cin>>bin6;
      printf ("3rd Char (2^2):  ");
      cin>>bin5;
      printf ("4th Char (2^3):  ");
      cin>>bin4;
      printf ("5th Char (2^4):  ");
      cin>>bin3;
      printf ("6th Char (2^5):  ");
      cin>>bin2;
      printf ("7th Char (2^6):  ");
      cin>>bin1;
      printf ("8th Char (2^7):  ");
      cin>>bin0;
      
      if ((bin7>1)||(bin6>1)||(bin5>1)||(bin4>1)||(bin3>1)||(bin2>1)||(bin1>1)||(bin0>1))
      {
           printf ("Only 1s and 0s. Please retry.\n\n");
           goto begin;
      }
      
      else
      {
           bin7=bin7*1;
           bin6=bin6*2;
           bin5=bin5*4;
           bin4=bin4*8;
           bin3=bin3*16;
           bin2=bin2*32;
           bin1=bin1*64;
           bin0=bin0*128;
           tot=bin7+bin6+bin5+bin4+bin3+bin2+bin1+bin0;
      }
      
      printf ("Decimal: %d\n",tot);
      system ("PAUSE");
}


Better One

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
/* Created by usandfriends */
#include <iostream>
#include "math.h"
using namespace std;

main()
{
      int bin,tot,i,n,j;
      
      tot=0;
      printf ("Binary to Decimal.\n");
      printf ("To what 2^nth is this binary?  ");
      cin>>n;
      
      for (i=0;i<=n;i++)
      {
          begin:
          cout<<"2^"<<i<<" char?  ";
          cin>>bin;
          if (bin==0)
          {
               tot=tot+0;
          }
          else if (bin==1)
          {
               for (j=0;j<i;j++)
               {
                    bin=bin*2;
               }
               tot=tot+bin;
          }
          else
          {
               printf ("Only 1s and 0s. Please retry.\n\n");
               goto begin;
          }
      }
      
      printf ("Decimal: %d\n",tot);
      system ("PAUSE");
}


Your friends are right here. Us and Friends. ;)
Do I have to save you again, ft95? ;)
Last edited on
Thank yoy usandfriends!!!!
I could solve my problem.
@ ft95
Change line 22 from this:
 
        binarrey[i]=num;

to this:
 
        binarrey[i] = num - '0';

That is, subtract the ASCII value of digit '0' from the value which was input.

The calculation of the result can be simplified. Although this works,
29
30
    for(power=0,counter=i;power<=i;power++,counter--)
        sum=pow(2,power)*binarrey[counter]+sum;

it would be more straightforward like this:
29
30
    for (counter=i; counter >= 0; counter--)
        sum = sum*2 + binarrey[counter];


One more comment, the variable sum does not need to be of type double. It will only ever contain an integer value.

----------------------------------------------------------------------------
I add the following as background information, if it doesn't quite make sense now, then don't worry too much.

If sum is an integer type, you could do this:
29
30
31
32
33
    for (counter=i; counter >= 0; counter--)
    {
        sum <<= 1;
        sum |= binarrey[counter];
    }


Although that looks different, it gives the same (correct) result. What this code does, in effect, is to take the binary digits which were input, and place each one into the sum. The 'or' operator |= puts the current binary digit (bit) into the right-most position. The left shift operator <<= then slides the whole value one position to the left, to make way for the next digit.

This is a fairly direct way of taking a binary input and forming the corresponding binary integer.
Last edited on
Thank you so much,chervil...
Last edited on
of course it is true:

sum =binarrey[counter]*2 + sum;

not this as you wrote.but thank you so much again.I liked this methode( binarrey[i] = num - '0');


sum = sum*2 + binarrey[counter];
wow,thank you.I didn't know that.
Pages: 12