Replacing user entered data and only accepting numbers

I need to make a program and part of the program needs to get the users social security number and display it with the numbers being replaced by X. For example, if the user entered. 555-55-5555, it would show XXX-XX-XXXX instead. Also, I do not know how to make it so that it will only accept numbers. I don't want the user to be able to enter something like 55A-BC-55AB. I'm very lost here and would appreciate some help. What I have so far doesn't work. It has an error on the >= and the &&. That section is my attempt to make it only accept valid social security numbers.

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

using namespace std;

int main()
{
	string socialSec;

	cout << "Social Security Number: ";
	cin >> socialSec;

	if (socialSec >= 000 - 00 - 0000) && (socialSec <= 999 - 99 - 9999)
		cout << "Social Security Number: ***-**-****";

	else
		cout << "invalid Social Security Number";

	return 0;
I adjusted it to this and it somewhat solved the problem of accepting valid SSN's. At catches most errors anyways... but it still shows the initial user input as well as the ***-**-****
for example, the user enters 555-55-5555 the program will look like this to the user:

Social Security Number: 555-55-5555
Social Security Number: ***-**-****


So I need to make it so the first line doesn't appear. Here is the adjusted code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

#include <iostream>
#include <string>

using namespace std;

int main()
{
	string socialSec;

	cout << "Social Security Number: ";
	cin >> socialSec;

	if (socialSec >= "000-00-0000" && (socialSec <= "999-99-9999"))
		cout << "Social Security Number: ***-**-****";

	else
		cout << "invalid Social Security Number";

	return 0;
}
Last edited on
There is no standard way to do this that I know of. There is however, conio.h which is deprecated so your compiler may not support it.

here is an example
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
#include<iostream>
#include<conio.h>
#include <string>
#include <Windows.h>


int main()
{
	std::cout << "Enter a social ";
	char tempChar = 'a';
	std::string tempString;

	tempChar = _getch();
	tempString += tempChar;
	while (tempChar != '\r')
	{
		std::cout << "*";
		tempChar = _getch();
		tempString += tempChar;

	}

	std::cout << "\nYou entered " << tempString << std::endl;

	std::cin.ignore(INT_MAX, '\n');
	return 0;
}



as for validation try the isdigit function
http://www.cplusplus.com/reference/cctype/isdigit/?kw=isdigit

you could also use a regular expression but that's probably overkill for this.
Last edited on
closed account (j3Rz8vqX)
will only accept numbers
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
#include <iostream>
#include <string>
#include <conio.h>
int main()
{
    char temp;
	std::string tempString;

	std::cout<<"Enter a social: ";
	while(tempString.size()<=10)
    {
        temp=getch();
        if(temp>='0'&&temp<='9')
        {
            tempString+=temp;
            std::cout<<'*';
            if(tempString.size()==3||tempString.size()==6)
            {
                tempString+='-';
                std::cout<<'-';
            }
        }
    }

	std::cout << "\nYou entered " << tempString << std::endl;

    std::cout<<"Press <enter> to exit console: ";
	std::cin.ignore(255, '\n');
	return 0;
}
Enter a social: ***-**-****
You entered 123-45-6789
Press <enter> to exit console:

There are other means to detecting keys such as windows:
GetAsyncKeyState(/*key*/)
but for this case, getch() was used.
Topic archived. No new replies allowed.