Insertind dashes in int.

I have to store ssn as an int but print it with dashes 000-00-0000. How do I go about with this. I know you can use substring for strings but using int is new.
Any help is appreciated.

Ex:
class student
{
private:
string FIRST;
string MIDDLE;
string LAST;
int SSN;
public:
******
};
Social Security Numbers should not be stored in a numeric type, just as phone numbers should not be stored in a numeric type.
Last edited on
Use String for the SSN, No reason to use int what so ever. For the how to ignore the dashes part, you could do something similar to

 
 if (SSN == '-') { continue; }


I did this in C, and havent tried it in C++ So Im not sure on how it works exactly.
@nick onfire

Hopefully, you're not using actual SS numbers, as it's a very bad idea, as LB says. But, if it's just for the program, and the numbers are not going to be stored, or saved in a file, you could go this route. Instead of an int, use char SSN[10];

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using std::endl;
using std::cout;

int main()
{
	char SSN[10] = "123456789"; // or fill it with a user input
	cout << "SS number : ";
	for (int x = 0; x < 9; x++)
	{
		if (x == 3 || x == 5)
			cout << "-";
		cout << SSN[x];
	}
	cout << endl << endl;
    return 0;
}
You could be cute and let C++ do this for you:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <locale>

struct ssn_out : std::numpunct<char> {
    char do_thousands_sep()   const { return '-'; }  // separate with -
    std::string do_grouping() const { return "\4\2\3"; } // 000-00-0000
};

int main()
{
    std::cout << "default locale: " << 123456789 << '\n';
    std::cout.imbue(std::locale(std::cout.getloc(), new ssn_out));
    std::cout << "ssn locale: " << 123456789 << '\n';
}


output:
1
2
default locale: 123456789
ssn locale: 123-45-6789
Haha, that's cute Cubbi
Topic archived. No new replies allowed.