-1

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.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
Chuah Cheng Jun
  • 243
  • 1
  • 3
  • 17
  • 2
    It depends, show us some code. – user35443 Jun 01 '15 at 18:45
  • 2
    that's a string formatting problem. all integers have infinite leading (and trailing) zeroes. if you wan them to be displayed, then you'll have to tell C to properly format the output. – Marc B Jun 01 '15 at 18:46
  • @MuertoExcobito no, it's not the way i want. I want it to print out fully same from my text file. – Chuah Cheng Jun Jun 01 '15 at 18:48
  • @ChuahChengJun - you should explain the actual problem in the question, because as it is written, it is a duplicate of the one I tagged. – MuertoExcobito Jun 01 '15 at 18:50
  • @MuertoExcobito edited. please refer. thanks – Chuah Cheng Jun Jun 01 '15 at 18:52
  • @Jarod42 for some reason, i need to use int for this. – Chuah Cheng Jun Jun 01 '15 at 18:54
  • You mention *"contact number"*. Are these telephone numbers? Telephone numbers are not integers and should not be handled as such. They are strings, which happen to contain digits. It sounds like you should be using strings. – Greg Hewgill Jun 01 '15 at 19:02
  • @GregHewgill just want to know, despite using string, is there any possible method? – Chuah Cheng Jun Jun 01 '15 at 19:31
  • 1
    @ChuahChengJun You can use any method you want. There is no "one right way". If you don't want to use a string, you could use another integer that codes the number of leading zeroes. You could even use base 11 if you want, and use the eleventh digit to represent a leading zero. Do it however you want. – David Schwartz Jun 01 '15 at 19:51

1 Answers1

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.

agnu17
  • 398
  • 2
  • 9