How can I preserve leading zeros when reading a number and printing it later?

微笑、不失礼 提交于 2019-12-20 07:42:45

问题


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.



来源:https://stackoverflow.com/questions/30580903/how-can-i-preserve-leading-zeros-when-reading-a-number-and-printing-it-later

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!