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)

New Topic/Question
Reply




MultiQuote



|