displaying integers

Hello, can anyone please explain why this does not display like i wanted it to.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

int main()
{
    int a=72;
    int b=072;
    
    cout << "a: " << a << endl;
    cout << "b: " << b << endl;
    
    cin.get();
    return 0;
}


it will display
a: 72
b: 58


what i want it to display was
b: 072
Starting an integer with a zero makes it an octal number (http://www.cplusplus.com/doc/tutorial/constants/).

Maybe someone with more experience can explain a good way how to print what you want. Perhaps making it a string instead would do as you want?
Let's start with the basics:
Though a computer is one of the most stupid things around, it does act like a human when dealing with unnecessary numbers. Think about it: say I make a statement such as this:
1
2
     double a = 92.01;
     std::cout << a;


What will be printed is
92.01
; it will not print
92.01000000000000
as the zero's are just a waste. The same goes for what you want. The number 1 is not written as 000001, even if you declare it as such.

You could do what you want done, two ways:
1.)Make a string of the number and print it
2.)Make a char array that contains only 0's, and changes to new numbers when given them (this would require more effort, as it would work like a stack)
Last edited on
Topic archived. No new replies allowed.