How can I preserve leading zeros when reading a nu

2019-03-06 16:21发布

问题:

I want to cout an integer starting 0. For example, 0123456789. But when I print it out, it only display 123456789 instead of 0123456789.

How can I solve this problem?

Below is my sample code:

sample.txt

0125961349
01359395930
019349130

I parse 1 of the contact number into an object, eg: 019349130. hence:

cout << cp.contactNum << endl;

and the final result is

19349130

This is not the result I want. And you can see I have different length for the integer, I cannot use the leading zero solution to solve it or else it will become something like 0019349130.

回答1:

you must parse the numbers into a string or const char* else you lose information about the heading 0. So the type of contactNum should be std::string instead of int.



标签: c++ integer cout