I have a social security program that I need finished. I have the code so far but I need to know how to make is where the user can only input numbers and no letters. This is what I have and I just do not know how to program only numbers.

`

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

int main()
{
    string ssn = "";
    cout<< "Social Security number without hyphens: ";
    getline(cin, ssn);

    if (ssn.length() == 9)
    {
        ssn.insert(3, "-");
        ssn.insert(6, "-");
        cout << "Social Security number: " << ssn << endl;
    }
    else
        cout << "The number must contain"
        << "9 characters" << endl;
    system("pause");
    return 0;
}`

The short answer is that you can't control the user. Accept any string and then validate that it's what your program can process. If it isn't discard the string and ask for another one.

The long answer is that you can do it, but it's more trouble than it's worth in a console mode program. The ideal would be to use a GUI where an input control only allows numbers, but that's probably too much at the moment.

I added some more to it but it still allows numbers to by input into the program how do I make it where it would be invalid.

`#include<iostream>
#include <string>
using namespace std;
char verifyNumeric(string)

int main()
{
    const string ALL_NUMBERS = "All Numbers";
    const string NOT_ALL_NUMBERS = "Not all numbers";

    string ssn = "";
    char isAllNumbers = ' ';

    cout<< "Social Security number without hyphens: ";
    getline(cin, ssn);

    if (ssn.length() == 9)
    {
        ssn.insert(3, "-");
        ssn.insert(6, "-");
        cout << "Social Security number: " << ssn << endl;
        isAllNumbers = verifyNumeric(ssn);

        if (isAllNumbers == 'Y')
            cout << ALL_NUMBERS << endl << endl;
        else
            cout << NOT_ALL_NUMBERS << endl << endl;
    }
    else
        cout << "The number must contain"
        << "9 characters" << endl;
    system("pause");
    return 0;
}
//*****function definitions*****
char verifyNumeric(string ssn)
{
    string currentChar = "";
    int x = 0;
    char isANumber = 'Y';
    while (x < 9 && isANumber == 'Y')
    {
        currentChar = ssn.substr(x, 1);
        if (currentChar >= "0"
            && currentChar <= "9")
            x += 1;
        else
            isANumber = 'N';
    }
    return isANumber;
}`
while (true) {
    cout << "Social Security number without hyphens: ";
    getline(cin, ssn);

    if (verifyNumeric(ssn) == 'Y') {
        break;
    }
    else {
        cout << "Invalid input, try again: ";
    }
}

you can use assert to verify the datatype entered if its a type int or char then make your decision. Very handy .

you can use assert to verify the datatype entered if its a type int or char then make your decision

assert shouldn't be used for input validation because if it fails, the program hard aborts. Assertions should only be used for cases that you expect to always be true in correct code; they're for helping to ensure that your code is indeed correct before shipping it. If an assert fails, it means the programmer screwed up, not the user.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.