Insertind dashes in int.

Feb 22, 2015 at 1:11am
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:
******
};
Feb 22, 2015 at 1:25am
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 Feb 22, 2015 at 1:26am
Feb 22, 2015 at 1:41am
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.
Feb 22, 2015 at 5:12am
@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;
}
Feb 22, 2015 at 5:17am
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
Feb 22, 2015 at 6:29am
Haha, that's cute Cubbi
Topic archived. No new replies allowed.