954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Base conversion program (C++)

I'm a little new to c++. I have written a code for a base conversion program. It handles base conversion between bases 2-36 and stops when the user enters "0". I have written the program, however I still need to make it convert whole numbers instead of just integers. Ex: 0.22 or 13.587 etc. I also need to be able to convert negative numbers. Can someone help me out?
Thanks in advance!


here is what I have so far.

int main()



{

int number,base,final[99];

double remainder;

cout<<"Enter the number system base (2--36): ";

cin>>base;



if( base<37&& base>1)

{

while (number !=0)

{

int i=0;

cout<<"Enter a decimal integer: ";

cin>>number;

if (number==0)

{

cout<<"bye!"<<endl;

break;

}

while (number !=0)

{

remainder=(number%base);

final[i]=remainder;



i++;

number=number/base;



}

for (i--; i>-1; i--)

{



if (final[i]<10)

cout<<final[i];

else



cout<<char(final[i]-10+'a');

}

number=1;

cout<<endl;

}

}

return 0;



}
jengah27
Newbie Poster
1 post since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

>>however I still need to make it convert whole numbers instead of just integers. Ex: 0.22 or 13.587 etc.

Whole numbers are integers. 0.22 and 13.587 are decimals. You may need to develop a more complex algorithm mathematically for this. I'm not sure that just using the % operator will work, though it may be part. I'd say work through a few of them with paper/pencil before tackling the program.

>>I also need to be able to convert negative numbers.

That's fairly easy. Figure out whether it's a negative number. If it is, take the negative sign away and make it positive. Convert the positive number, then stick the negative sign back in front.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: