Skip to content

Instantly share code, notes, and snippets.

@dioncodes
Created December 5, 2011 10:13
Show Gist options
  • Save dioncodes/1433112 to your computer and use it in GitHub Desktop.
Save dioncodes/1433112 to your computer and use it in GitHub Desktop.
A simple programm to convert decimal to binary numbers
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int decimaltobinary() {
string ergebnis;
int erg;
int eingabe;
cout << "Enter the decimal number (integer): ";
cin >> eingabe;
erg = eingabe;
while(erg != 0) {
ergebnis = char(erg % 2 + '0') + ergebnis;
erg = erg / 2;
}
if(eingabe == 0) {
ergebnis = "0";
}
if(eingabe < 0) {
ergebnis = "Invalid input.";
}
cout << "Binary: " << ergebnis << endl << endl;
//cout << endl << endl;
erg = 0;
eingabe = 0;
ergebnis = "";
cout << "Start program again? (y/n): ";
}
int main() {
cout << "Welcome to the Converter (Decimal to binary)" << endl;
bool repeatprog = true;
while(repeatprog) {
decimaltobinary();
char repeat;
cin >> repeat;
if(repeat == 'y') {
}
else {
cout << "Copyright 2011 Dion P. - http://drp-seiten.de/" << endl << endl;
repeatprog = false;
system("PAUSE");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment