I had an exercise to convert from decimal to binary with base 2, it was simple simple i did the following:
Code:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
void Conversion (int n);
int main () {
int n;
n=-1;
cout<< "Please enter a positiover integer";
do{
if(!(cin>>n)){
cin.clear();
cin.ignore(255, '\n');
}
if ( n<0 ){
cout<<"error, Please enter positive integer."<<endl;
}
}while ( n<0 );
cout << n<<
" converted to binary is "<<endl;
Conversion(n);
}void Conversion(int n) {
using namespace std;
float remainder;
if(n <= 1) {
cout << n;
return ;
}
remainder = n%2;
Conversion(n >> 1);
cout << remainder;
}
I then had an follow up exercise which was to replicate but for any base up to 10, i thought i would just have to replace 2 with a variable obtained from the user, however this did not work as i got an error saying too few arguments function but i cannot see why i am getting this.
Code:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
float Conversion (int n, int b);
int main () {
int n;
n=-1;
int b;
b=0;
cout<< "Please enter a positiover integer";
do{
if (!(cin>>n)){
cin.clear();
cin.ignore(255, '\n');
}
if ( n<0 ){
cout<<"error, Please enter positive integer."<<endl;
}
}while ( n<0 );
cout <<"please enter the base";
do{
if (!(cin>>b)){
cin.clear();
cin.ignore(255, '\n');
}
if (b<2 || b>10){
cout<<"error please enter a number 1-10";
}
}while (b<2 || b>10);
cout << n<<
" converted to binary is "<<endl;
Conversion(n,b);
}
float Conversion(int n,int b) {
using namespace std;
float remainder;
;
if (n <= 1) {
cout << n;
return 0 ;
}
remainder = n%b;
Conversion(n >> 1);
cout << remainder;
}
any help will be greatly appreciated, thanks