# bin. and dec.

Page 1 of 1

## 7 Replies - 7408 Views - Last Post: 04 December 2001 - 12:17 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=2413&amp;s=72ccf8a6a0fdd40215606d130f596a04&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 monkey

Reputation: 0
• Posts: 46
• Joined: 24-August 01

# bin. and dec.

Posted 29 November 2001 - 09:37 AM

convert binary numbers to decimal numbers, and vice versa.
What I can't get it to do is convert either of them. Could
someone please look at it for me to see what I did wrong.
Here is the code :
```
#include <iostream.h>
#include <fstream.h>
#include <windows.h>void title();
void do_again();
void getdec();
void getbin();
void calcdec();
void calcbin();int yn;
int decimal, binarys;
ofstream out;int main()
{
title();
return 0;
}
void title()
{
out.open("Binary.txt",ios::out);
cout<<"C++ Decimal Binary Converter - Jeff Golik
";
out<<"C++ Decimal Binary Converter - Jeff Golik
";
cout<<"Advance C++ - Block 2 - Mr. Hanosh
";
out<<"Advance C++ - Block 2 - Mr. Hanosh
";
cout<<"Program 7 - Decimal-Binary Conversions
";
out<<"Program 7 - Decimal-Binary Conversions
";
cout<<"Purpose : Convert decimal numbers to binary numbers
";
out<<"Purpose : Convert decimal numbers to binary numbers

";
do_again();
}
void do_again()
{
cout<<"

Oh masterful ASCII humanoid, what would you like to do?
";
cout<<"Please type a 0 to exit.
";
cout<<"Please type a 1 to convert decimal numbers to binary numbers
";
cout<<"Please type a 2 to convert binary numbers to decimal numbers
";
cin>>yn;
switch (yn)
{
do
{
case 1:
{
getdec();
break;
}
case 2:
{
getbin();
break;
}
case 0:
{
system ("cls");
cout<<"Thank you for coming ASCII master - Please come again.
";
exit (0);
break;
}
}while (yn != 'n');
}
}
void getdec()
{
cout<<"

Oh ASCII humanoid, please enter a decimal number :
";
out<<"

Oh ASCII humanoid, please enter a decimal number :
";
cin>>decimal;
out<<decimal;
calcdec();
}
void getbin()
{
cout<<"
Now Oh ASCII master, please enter a binary number (a combination of 8 0s and 1s)
";
out<<"
Now Oh ASCII master, please enter a binary number (a combination of 8 0s and 1s)
";
cin>>binarys;
if ((binarys > 11111111)||(binarys < 00000000))
{
cout<<"That number does not exist - please try again!
";
getbin();
}
else
{
calcbin();
}
}
void calcdec()
{
int a, ans;
for(a=0;a<=8;a++)
{
ans=decimal%2;       //calculates decimal number to get binary number
if ((ans == 1)||(ans > 1))
{
cout<<" 1
";
}
else
{
cout<<" 0
";
}
}
cout<<"
Here is your Decimal to Binary conversion
";
do_again();
}
void calcbin()
{
int b, num[8];
for (b=0; b<=8; b++)
{
binarys=num[b];
}
if((num[0]*128)+(num[1]*64)+(num[2]*32)+(num[3]*16)+(num[4]*8)   //calculates 0s & 1s
+(num[5]*4)+(num[6]*2)+(num[7]*1))                           //to get decimal #
{
cout<<num[0]*128+num[1]*64+num[2]*32+num[3]*16+num[4]*8
+num[5]*4+num[6]*2+num[7]*1<<'
';
}
do_again();
}
```

(Edited by monkey at 11:40 am on Nov. 29, 2001)

Is This A Good Question/Topic? 0

## Replies To: bin. and dec.

### #2 malkiri

• D.I.C Regular

Reputation: 3
• Posts: 364
• Joined: 29-March 01

## Re: bin. and dec.

Posted 29 November 2001 - 12:17 PM

Okay...in your decimal to binary conversion, try doing this to see what's wrong: Pick some fairly easy decimal number, say 5, and run through your code. See what you come up with, and you'll probably spot where the problem is. Do the same thing with your binary to decimal conversion. Keep columns of values for each variable, and write down what each variable holds at each step in the functions, and be sure you don't make any assumptions - do exactly what your code is doing.

### #3 monkey

Reputation: 0
• Posts: 46
• Joined: 24-August 01

## Re: bin. and dec.

Posted 30 November 2001 - 09:25 AM

When I type in any decimal number, the binary conversion
comes up as 11111111 only, I can't get it to do anything else.

### #4 malkiri

• D.I.C Regular

Reputation: 3
• Posts: 364
• Joined: 29-March 01

## Re: bin. and dec.

Posted 30 November 2001 - 10:44 AM

Don't run your program. Pretend you can't compile for the next 15 minutes - all you have is a piece of paper and a printout of your code.
List the variable names across the top of the paper. Write down whatever the intial values of the variables are. Now step through your code, line by line, and as you change the variables, write down what the new value is under the previous value.
The parts of your code that you should be focusing on (ie. where the problem is) is the for loops inside calc_bin() and calcdec().

### #5 monkey

Reputation: 0
• Posts: 46
• Joined: 24-August 01

## Re: bin. and dec.

Posted 03 December 2001 - 09:05 AM

Thanks, that worked out. I found my problem

### #6 malkiri

• D.I.C Regular

Reputation: 3
• Posts: 364
• Joined: 29-March 01

## Re: bin. and dec.

Posted 03 December 2001 - 02:52 PM

Excellent. :)

### #7 monkey

Reputation: 0
• Posts: 46
• Joined: 24-August 01

## Re: bin. and dec.

Posted 04 December 2001 - 09:46 AM

Now I'm confused,
here's some code to look at :
```void calcdec()    //calcdec function
{system ("cls");
int i, num[8], ans[8];//integer declarations
cout<<"The decimal number that you want converted : "<<decimal<<"
";
cout<<"
Here is your Decimal to Binary conversion :
";
for(i=0; i<=7; i++)
{
num[i]=decimal%2;
ans[i]=num[i]/2;
cout<<ans[i];
}
```

This is now my calculate decimal code. Where you enter a decimal number, and it changes to binary. I can't figure out why it won't work. It just prints out 0's.

### #8 malkiri

• D.I.C Regular

Reputation: 3
• Posts: 364
• Joined: 29-March 01

## Re: bin. and dec.

Posted 04 December 2001 - 12:17 PM

Think about what you're doing to the num[] array, particularly in:
```num = decimal%2;

```