7 Replies - 7057 Views - Last Post: 04 December 2001 - 12:17 PM Rate Topic: -----

#1 monkey  Icon User is offline

  • New D.I.C Head

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

bin. and dec.

Posted 29 November 2001 - 09:37 AM

Please help, the program I am writing is supposed to
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  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 3
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 monkey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 malkiri  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 3
  • View blog
  • 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().
Was This Post Helpful? 0
  • +
  • -

#5 monkey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#6 malkiri  Icon User is offline

  • D.I.C Regular
  • member icon

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

Re: bin. and dec.

Posted 03 December 2001 - 02:52 PM

Excellent. :)
Was This Post Helpful? 0
  • +
  • -

#7 monkey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#8 malkiri  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 3
  • View blog
  • 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;


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1