By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,998 Members | 2,773 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,998 IT Pros & Developers. It's quick & easy.

c++ decimal convert to octal, hex, binary, (needs a reverse cout)

P: 1
//Create a program that will accept numbers in decimal and display them in either binary, octal or hexadecimal. Use a switch statement to select the option (binary, octal, or hex) picked from the menu and make use of modulus (%) and divide operators.
#include<iostream>
using namespace std;
int menu();
void toBinary();
void toOctal();
void toHex();

int main()
{
int choice = menu();
switch(choice)
{
case (1):
toBinary();
break;
case (2):
toOctal();
break;
case(3):
toHex();
break;
}
return 0;
}
int menu()
{
int choice;
cout << "(>^.^)>==(_)Menu(_)==<(^.^<)" << endl;
cout << "Convert the number from decimal into: " << endl;
cout << "1 Binary" << endl;
cout << "2 Octal" << endl;
cout << "3 Hexadecimal" << endl;
cin >> choice;
return choice;
}

void toBinary()
{
int num;
int total = 0;
cout << "Please enter a decimal: ";
cin >> num;
if(num==1)

{
cout<<"\nMAJOR ERROR USE A NUMBER!!!!!";
}
else
while(num > 0)
{
total = num % 2;
num /= 2;
cout << total << " ";
}
}

void toOctal()
{
int num;
int total = 0;
cout << "Please enter a decimal: ";
cin >> num;
if(num==1)
{
cout<<"\nMAJOR ERROR USE A NUMBER!!!!!";
}
else
while(num > 0)
{
total = num % 8;
num /= 8;
cout << total << " ";
}

}
void toHex()
{

int num,counter,x,a,hex[100];
cout<<"Please enter a decimal: ";
cin>>num;
cout<<"\nThe Decimal number "<<num<<" converts to the Hexadecimal number: ";
for(counter=0;num!=0;counter++)
{
a=num%16;
hex[counter]=a;
num=num/16;
}
for(x=counter-1;x>=0;x--)
{
if (hex[x] > 9)
cout << char('A'+hex[x]-10);
else
cout << hex[x];
}
cout << endl;
}


the results need to be reversed in cout but i do not know how. can you guys please help me go through with this? thank you for taking the time to replying to this post.
Dec 17 '10 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
You are putting your results in an array. Why not just display the array backwards at the end of the program rather than displaying as you go along?
Dec 17 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.