numerical systems

I want to write a program , that shows numerical systems in "10" in "8" , "16" , "2" systems , I've done in "10" so far and I'm confused how to do in other systems.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

int main()
{
    int x;
    for(x=0;x<=15;x++)
 
    cout << (x) << '\t';
    cout << '\n';

    return 0;
  }

I don't know if it's ok , but I need help.
Numerical bases? Check this out.

http://www.cplusplus.com/doc/hex.html
You can use the flags dec, hex and oct to convert them automatically to Decimal, Hexadecimal and Octal.
For binary you have to build your one function to convert it.
Here is an example how dec, hex and oct works:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <iomanip>
using namespace std;

int main(){
   int num = 145;
   cout << "oct: " << oct << num << '\n';
   cout << "dec: " << dec << num << '\n'; //If you change the flag to oct or hex you must
   // put it back to dec to show decimal again...
   cout << "hex: " << hex << num << '\n';
}


You can read for these flags here:
http://www.cplusplus.com/reference/iostream/manipulators/dec.html

Hope this helps
Last edited on
Thanks for that Mitsakos, I didn't know that. There was an example in a book a was reading that converted numbers over but used modulus and then left or right shift or something. I thought it was a bit more complicated then that. Cool.
Thanks , It really helped.
as mentioned in chapter 2 of "thinking in C++ (v1)"
as mentioned in chapter 2 of "thinking in C++ (v1)"


What about it?
Last edited on
Topic archived. No new replies allowed.