How To Print Hyphens on ID Number?

How can I get an ID number read from a file to print out as xxx-xx-xxxx ?
it is read as numbers from the file but how can I get it to print with hyphens using modulus?
I've tried printing numbers per line etc. before but I'm not sure how to print hyphens on an ID number. Thanks in advance
Last edited on
If your ID is a number, like an SSN, use the std::numpunct. Here is a modified version of http://www.cplusplus.com/forum/general/65769/#msg355996

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
35
36
37
#include <locale>
#include <string>

template <typename CharT>
struct ssn_punct: std::numpunct <CharT>
  {
  protected:
    CharT do_thousands_sep() const 
      { 
      return '-'; 
      }
      
    std::basic_string <CharT> do_grouping() const 
      { 
      return "\4\2\3\0"; 
      }
  };

#include <iomanip>
#include <iostream>
using namespace std;

int main()
  {
  locale loc( locale::classic(), new ssn_punct <char> );
  cin.imbue( loc );
  cout.imbue( loc );

  unsigned n;
  cout << "Please enter the SSN " << 123456789 << "> ";
  cin >> n;

  if (n == 123456789) cout << "Good job!\n";
  else                cout << "Fooey!\n";

  return 0;
  }

Of course, now all numbers on your stream will be formatted that way. If that is too much, you can write a couple of methods to imbue on a stringstream, or a specialized SSN type with overloaded I/O operators, or just a manipulator to do it, along the lines of this post http://www.cplusplus.com/forum/beginner/13044/#msg62827 (you don't need the word mutable anymore).

Good luck!
Here's something for you to play with.

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iomanip>
#include <iostream>
using namespace std;

//----------------------------------------------------------------------------
template <typename T>
struct ssn_t
  {
  T& n;
  explicit ssn_t( T& n ): n( n ) { }
  ssn_t( const ssn_t <T> & i ): n( i.n ) { }
  };

//----------------------------------------------------------------------------
template <typename T>
inline
ssn_t <T>
ssn( T& n )
  {
  ssn_t <T> result( n );
  return result;
  }

//----------------------------------------------------------------------------
template <typename T>
istream& operator >> ( istream& ins, const ssn_t <T> & ssn )
  {
  ssn.n = 0;
  string inputs = "0123456789-";
  while (inputs.find( ins.peek() ) != inputs.npos)
    {
    if (ins.peek() == '-') ins.get();
    else ssn.n = (ssn.n * 10) + (ins.get() - '0');
    }
  // If you wanted, you could perform some SSN validation here, but there is 
  // a limit, as an SSN's validity also depends on the issue date.
  return ins;
  }

//----------------------------------------------------------------------------
template <typename T>
ostream& operator << ( ostream& outs, const ssn_t <T> & ssn )
  {
  ostream::fmtflags flags = outs.flags();
  outs << setfill( '0' ) << setw( 3 ) << (ssn.n / 1000000 % 1000)  << "-";
  outs << setfill( '0' ) << setw( 2 ) << (ssn.n / 10000   % 100)   << "-";
  outs << setfill( '0' ) << setw( 4 ) << (ssn.n           % 10000);
  outs.flags( flags );
  return outs;
  }

//----------------------------------------------------------------------------
int main()
  {
  unsigned n;

  cout << "Please enter an SSN> " << flush;
  cin >> ssn( n );
  while (!cin)
    {
    cin.clear();
    cout << "Please, enter only an SSN> " << flush;
    cin >> ssn( n );
    }

  cout << "Good job!\n"
          "You entered the SSN " << ssn( n ) << endl;
  cout << "(or " << n << ")\n";

  return 0;
  }
@Duoas gave an excellent idea about using the numpunct function, but like he said all of your numbers in the stream will be formatted like that.

Another option would be to convert number to a string and insert some dashes into the string. Afterwards, just print it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void PrintSSN(int _ssn)
{
    // Note: std::to_string() was introduced in C++0x.
    string ssn_str = to_string(_ssn);
    
    ssn_str.insert(3, '-');
    ssn_str.insert(6, '-');

    cout << ssn_str;
}

int main()
{
    int number = 123456789;
    cout << "My SSN is: " << PrintSSN(number) << endl;

    cin.get();
    return 0;
}

Please note that I have not compiled this, so there might be some typing errors.

My SSN is: 123-45-6789


EDIT: Added output.
Last edited on
Thanks guys
Topic archived. No new replies allowed.