473,474 Members | 1,304 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

1 New Member
//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
1 13001
weaknessforcats
9,208 Recognized Expert Moderator Expert
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

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

Similar topics

7
by: Golan | last post by:
Hi, I need to convert a Binary value to Decimal. I've been told that the value is an unsigned one. How can I do this? I use memcpy into an unsigned char variable, but when I print the value I got...
1
by: Stan | last post by:
The convert method allows us to change any base number (bin, octal, hex ) to decimal, but how can I change a decimal value back to an octal number??? Thanks in advance Stan
2
by: marulasiddesh | last post by:
can any one help me out in converting a decimal number to binary number?
2
by: Hets | last post by:
Write a C program which can convert decimal number into binary number.
1
by: svnlakshmi | last post by:
Pls help me out in this aspect--How to convert a binary file to a file in text format using C?
0
by: Terry Reedy | last post by:
A. Joseph wrote: These are number representation systems that can be applied to or used with integral, rational (numberator,denominator), and 'point' numbers. Try Wikipedia or any search...
0
by: jcarter | last post by:
How can I create a CodeDOM statement to accomplish the following this.pictureBox.Image = <Convert the binary value of the picture from the XML file and display> I have tried the following...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.