Mar 23, 2012 at 9:45pm UTC
This will compile:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
double binary(int number);
double read_int(string prompt)
{
bool done = false ;
double returnvalue = 0.0;
while (done==false ){
cout<<prompt;
cin>>returnvalue;
if (cin.fail()==0){
//done=true;
if (returnvalue>=0.0){
done=true ;}
else {
cout<<"error.must be >=0.0" <<endl;
}
}
else {
cin.clear();
cin.ignore(INT_MAX,'\n' );
cout<<"error cannot read input" <<endl;}
}
//while loop
return returnvalue;
}
/***************************************************************************************/
int main(){
int number;
cout << "Please enter a number: " ;
cin >> number;
if (number < 0)
cout << "That is not a positive integer.\n" ;
else {
cout << number << " converted to binary is: " << binary(number) << endl;
double base=read_int("Please enter a base from 2 to 35: " );
//while (35>=base>=2) // infinite loop
}
return 0;
}
/***************************************************************************************/
double binary(int number) {
int remainder;
int base = 2;
if (number <= 1) {
cout << number;
return 0;
}
remainder = number%base;
binary(number >> 1);
cout << remainder;
if (remainder >= 10)
printf("A" );
else if (remainder==11); // nothing here
else if (remainder==12)
printf("C" );
else if (remainder==13)
printf("D" );
else if (remainder==14)
printf("E" );
else if (remainder==15)
printf("F" );
return 0; // have to return something
}
Though there are a bunch of things that won't work right (or at all).
Last edited on Mar 23, 2012 at 9:47pm UTC
Mar 24, 2012 at 12:03am UTC
First, I suspect you'll need to use BASE as a fnc arg inside BINARY()
Second, try using a SWITCH, so as it easier to read.
Third: double remainder(remainder);
(A) it's usually considered confusing to name things with the same name.
(B) it's not considered good form to declare a fnc that isn't used.
Fourth: why is double read_int(string prompt)
named read_int when it accepts a string ?
Lastly, Try to indent maybe 3 spaces to the right.
While all these suggestions actually do nothing to help fix the error(s), they
will help to isolate the error(s).
Last edited on Mar 24, 2012 at 12:04am UTC