Converting a integer with leading or trailing zeros to a string

Hello guys! First time posting! I am trying to have a user input ID information for a student, and for the ID it could be for example:
00000001
00123000
12345678

I know the to_string function is supposed to convert a number to an integer, but this seems to fail and I think it is due to the zeros in the input. I also tried this code below, but it still seems to fail.

1
2
3
4
5
6
7
8
9
10
11
12
13
  int count = 0;
  while (ID != 0) {
     if (ID % 10 == 0)
     {
          count++;
          ID= ID/ 10;
     }
     else
     {
          ID= ID/ 10;
          count++;
     }
}


My count is to just count how many digits are added for the ID (It has to be an 8 digit length)

Any suggestion on how to convert a number to a string which includes trailing and leading zeros
Last edited on
I believe you are asking how to convert an int into a string with a field width of 8, padded with leading zeroes. Here are three possibilities:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <sstream>
#include <string>

std::string my_to_string(int n, int width = 8)
{   // this version will truncate any digits past the width
    std::string s(width, '0');
    for (int i = width; i--; n /= 10) s[i] = char('0' + n % 10);
    return s;
}

std::string my_to_string2(int n, int width = 8)
{
    auto s = std::to_string(n);
    if (int(s.size()) < width) s.insert(0, width - s.size(), '0');
    return s;
}

std::string my_to_string3(int n, int width = 8)
{
    std::ostringstream oss;
    oss.width(width);
    oss.fill('0');
    oss << n;
    return oss.str();
}

int main()
{
    int id = 12300;
    std::cout << my_to_string(id) << '\n';
    std::cout << my_to_string2(id) << '\n';
    std::cout << my_to_string3(id) << '\n';
}

Output:
00012300
00012300
00012300

Last edited on
I am sorry I should have clarified better.

Say someone inputs 00001234 or 00234000 --> I have to check if this is a valid 8 digit input.
I tried to convert to_string, but it does not work for trailing or leading zeros so I can not check if the user inputted a correct ID of length 8.

I basically take the number 00001234 and check to see if that number that was inputted is indeed 8 digits or not
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <string>
#include <cctype>

bool is_valid_input( const std::string& input )
{
    // invalid if the input does not contains exactly eight characters
    if( input.size() != 8 ) return false ;
    
    // invalid if any character in the input is not a decimal digit
    for( unsigned char uc : input ) if( !std::isdigit(uc) ) return false ;
    
    return true ; // valid (eight decimal digits)
}
How do you actually read the input. to_string doesn't bother about leading zeros.
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>

int main()
{
  std::cout << "Enter a number: ";
  int num;
  std::cin >> num;
  std::cout << "The number you entered: " << std::to_string(num) << '\n';
}

Output:

Enter a number: 00001234
The number you entered: 1234
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
using namespace std;

bool isValidID( string &id )
{
   cout << "Please enter an 8-digit ID: ";
   getline( cin, id );
   return id.size() == 8 && id.find_first_not_of( "0123456789" ) == string::npos;
}

int main()
{
   string id;
   while( !isValidID( id ) );
}
Hello tristanbox09,

I think it is time to post the whole program so everyone can see what you have done.

Without see what you have done no one knows what you started with.

As thmm demonstrated a numeric type variable does not store leading (0) zeros. A string will.

I think you may be misunderstanding the "std::to_string()" function. This converts a numeric variable to a string, but since the numeric variable does not store leading (0) zeros they do not show up in the string.

I have an idea, but it would help everyone to know how you are getting your input to start with.

Andy
You need to think of the ID as a string, not a number. It's just a little special because the string is made up of digits. So read the ID as a string and then validate it the way JLBorges shows.
Topic archived. No new replies allowed.