Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <String>
- using namespace std;
- void DecToBin(int num, int base);
- double rem;
- int final[99];
- int i = 0.0;
- int main()
- {
- double DecimalNum, base;
- cout << "Welcome to Binary conversion program!" << endl;
- cout << "Enter your base: ";
- cin >> base;
- while (base < 2 || base > 35)
- {
- cout << "Between 2~35 please :)" << endl;
- while(!(cin >> base)) // Loops that goes on until cin fails
- {
- cin.clear(); // if fails, clear the failed flag.
- while(cin.get() != '\n'){} // A loop the cleans out anything that is left over in the input stream.
- cout << "Only numbers please: " << endl << "What's the base again? " <<endl;// Output failed prompt. And requires only numbers
- }
- }
- cout << "Enter number in decimal: ";
- cin >> DecimalNum;
- DecToBin(DecimalNum, base);
- return 0;
- }
- void DecToBin(int num, int base)
- {
- while (num != 0)
- {
- rem = (num%base);
- final[i] = rem;
- i++;
- num = num/base;
- }
- for (i--; i>-1; i--)
- {
- if (final[i]<10) // if the remainder goes up to 10 then it changes the value to letters..
- cout << final[i];
- else
- cout << char(final[i]-10+'a');
- }
- num = 1;
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment