Is this a good sub for programming help?

19    16 Dec 2015 06:42 by u/im_the_dude_man

Hey everyone, I'm currently in school and taking a c++ class. I'm having a bit of trouble on one of my last projects and was hoping my fellow goats could lend a hand.

Just wanted to make sure that was acceptable here as the subs that are more geared for that haven't had activity in months. Please just let me know before I go spamming my noob code all over the place.

17 comments

2

@Mercuenomia

The commented out code is where I am having trouble, I commented it out just so I could move on testing my program.

//Function to check Social Security Number string ssidCheck(string) { string input, a, b, c, d, e, f, g, h, j; bool checkInput; int len, numA, numB, numC, numD, numE, numF, numG, numH, numJ;

checkInput = true;
//Loop to verify proper ssid entry
do
{
    cout << "Please enter your Social Security number as 123-45-6789: ";
    getline(cin, input);
    len = input.length();
    if (len < 1 || len > 11)
    {
        cout << "\nImproper entry. Please try again 0.\n";
        checkInput = false;
    }
    for (int i = 0; i < input.length(); i++)
    {
        if (input[i] == ' ')
        {
            cout << "\nImproper entry. Please try again 1.\n";
            checkInput = false;
            break;
        }
        else if (input[i] < '0' && input[i] > '9' && input[i] != '-')
        {
            cout << "\nImproper entry. Please try again 2.\n";
            checkInput = false;
            break;
        }
        else if (input[i] == '-' && i != input.at(3))
        {
            cout << "\nImproper entry. Please try again 3.\n";
            checkInput = false;
            break;
        }
        else if (input[i] == '-' && i != input.at(6))
        {
            cout << "\nImproper entry. Please try again 3.5.\n";
            checkInput = false;
            break;
        }
        else if ((i == input.at(3) && input[i] != '-') || (i == input.at(6) && input[i] != '-'))
        {
            cout << "\nImproper entry. Please try again 4.\n";
            checkInput = false;
            break;
        }
        */
        if (checkInput == true)
        {
            a = input.at(0);
            b = input.at(1);
            c = input.at(2);
            d = input.at(4);
            e = input.at(5);
            f = input.at(7);
            g = input.at(8);
            h = input.at(9);
            j = input.at(10);
            numA = stoi(a);
            numB = stoi(b);
            numC = stoi(c);
            numD = stoi(d);
            numE = stoi(e);
            numF = stoi(f);
            numG = stoi(g);
            numH = stoi(h);
            numJ = stoi(j);
            if ((0 == numA + numB + numC) || (0 == numD + numE) || (0 == numF + numG + numH + numJ))
            {
                cout << "\nImproper entry. Please try again.\n";
                checkInput = false;
                break;
            }
            else if (numA == 6 && numB == 6 && numC == 6)
            {
                cout << "\nImproper entry. Please try again.\n";
                checkInput = false;
                break;
            }
            else if (numA == 9)
            {
                cout << "\nImproper entry. Please try again.\n";
                checkInput = false;
                break;
            }
        }
    }
} while (checkInput == false);
return input;

}

4

What you're looking for here is

if (i == 3 && input[i] != '-')

4

Also, I don't believe there's any benefit to explicitly declaring 7 strings for the 7 digits, as you can do it all in one line as

numA = stoi(input[0])

or numA = stoi(input.at(0)) if you prefer, I'm fairly certain they're functionally equivalent

1

Hmm I did not think of that, thank you. I'll try that out.

edit: I'm getting an error when I try that.

0

I think I found the issue. You want a standalone string with a single digit in each of those variables. And by explicitly moving each character into it's own string, it add's a null termination character and gives each digit it's own location in memory. stoi takes a const string location, and reads until a null terminator, so my optimization actually doesn't help you. There's probably a way to do this as a one-liner, but it's not necessary. Would just look more elegant imo...

4

Oh man......I think I just gave myself permanent brain damage from that facepalm.

Thank you.

2

You are welcome, fellow goat.

1

Lol I know that feeling. As someone who has worked in the field for a little while I can say one of the most common things to happen when troubleshooting is missing the obvious. The first thing I do when I become stumped on a issue is step away for a little while. Stretch the legs, get some coffee, etc. Then come back and try to take a fresh look at the problem. A lot of times this works. When it does not then comes plan b; i.e. ask for help, it's amazing what a fresh pair of eyes can see as you just learned. Best of luck with your class. A C++ course in a EE major is what got me to switch my focus to comp sci!

1

I'd love to see code on this sub. Granted I'm a c# dev, so I may not be able to help with a specific c++ question :P

1

Heh, any help would be greatly appreciated. I'm down to like 2 projects due at the end of the week now.

1

/v/ProgrammingHelp might be the sub you look for. But it is practically unused.

0

Yeah, that's why I asked here real quick. This is probably going to be the only time I do this here and if anything I'll try to drum up some activity over in /v/ProgrammingHelp. I just have some tight time constraints.

0

"Over the last weeks I've been seeing buzzfeed doing" Yup, that's it, I'm done.