如何在读取数字并稍后打印时保留前导零? [英] How can I preserve leading zeros when reading a number and printing it later?
问题描述
我想 cout
一个从0开始的整数。例如,0123456789。
但是当我打印出来时,它只显示 123456789
而不是 0123456789
。
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?
以下是我的示例代码:
sample.txt
0125961349
01359395930
019349130
我解析了1个联系电话到一个对象,例如:019349130。因此:
I parse 1 of the contact number into an object, eg: 019349130. hence:
cout << cp.contactNum << endl;
,最终结果是
19349130
这不是我想要的结果。并且您可以看到我对整数有不同的长度,我不能使用前导零解决方案来解决它,否则它会变成类似 0019349130
。
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
.
推荐答案
你必须将数字解析成字符串或const char *否则你会丢失有关标题0的信息。所以contactNum的类型应该是std :: string而不是int。
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.
这篇关于如何在读取数字并稍后打印时保留前导零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!