-1

I am currently coding a program that checks a password to see if it is valid. The password must be 7 character and have an uppercase and lowercase letter. My current code can reach the first check - length - but then has a debug assertion failure. I believe that I need to fix the initial strings, but am not sure. Any help would be greatly appreciated.

// Lab 9 
// coded by Elijah

#include <iostream>
#include <string>
#include <iomanip>
#include <cctype>
#include <cstring>
using namespace std;

string checkCharacter(string);
string checkCharacterTwo(string);

int main()
{
    string capitalAnswer = "O", capitalAnswerTwo = "O";
    std::string password;

    cout << "Please enter a password: ";
    cin >> password;

    if (password.length() >= 7)
    {
        capitalAnswer = checkCharacter(password);

        if (capitalAnswer == "y")
        {
            string checkCharacterTwo(password);

            if (capitalAnswerTwo == "y")
            {
                cout << "Thank you, that is a valid password" << endl;
                return 0;
            }
            else
            {
                cout << "Passwords must contain at least one lowercase letter" << endl;
                main();
            }
        }
        else
        {
            cout << "Passwords must include at least one uppercase letter" << endl;
            main();
        }
    }
    else
    {
        cout << "Passwords must be at least 7 characters long" << endl;
        main();
    }
}

string checkCharacter(string password)
{
    string answer;
    string character;
    string capitalAnswer;
    int number;
    for (number = 0; number > password.length() || answer != "y"; number++)
        {
            character = password[number];

            if (character >= "A" && character <= "Z")
                answer = "y";
            else
                answer = "n";
        }

        {
            if (answer == "y")
                capitalAnswer = "y";
            else
                capitalAnswer = "n";
        }

        return capitalAnswer;
}

string checkCharacterTwo(string password)
{
    string answer;
    string character;
    string capitalAnswerTwo;
    for (int number = 0; number > password.length() || answer != "y"; number++)
    {
        character = password[number];

        if (character >= "a" && character <= "z")
            answer = "y";
        else
            answer = "n";
    }

    if (answer == "y")
        capitalAnswerTwo = "y";
    else
        capitalAnswerTwo = "n";

    return capitalAnswerTwo;
}
Elijah
  • 15
  • 2
  • 4
    Don't call main() ever. http://stackoverflow.com/a/2128727/487892 – drescherjm Nov 26 '14 at 19:50
  • 1
    `string checkCharacterTwo(password);` this isnt calling your function `checkCharacterTwo` this is creating a new string variable called `checkCharacterTwo` that is an exact copy of the `password` variable. – Borgleader Nov 26 '14 at 19:51
  • I just want to reiterate that It's a really bad idea to call main. Also it doesn't like like you need to include `` so don't include it. – shuttle87 Nov 26 '14 at 19:51
  • @Borgleader never heard of the most vexing parse ? – Christophe Nov 26 '14 at 20:11
  • 1
    @Christophe MVP is irrelevant here, he *meant* to call the function. Look two lines above where he calls `checkCharacter`. To note, MVP is the reverse, declaring a function when you meant to create a variable. He created a variable instead of calling a function. – Borgleader Nov 26 '14 at 20:13
  • @Christophe: Yes. Have you? Hint: it has _nothing_ to do with any of this code. – Lightness Races in Orbit Nov 26 '14 at 20:17
  • @Borgleader oh sorry ! I thought you were refering to the function used in the assignment expression, before realizing that there was no assignment ! – Christophe Nov 26 '14 at 20:38

1 Answers1

0

It appears that your looping condition in checkCharacter() is wrong and makes you go out of bounds. Correct it as foolows:

for (number = 0; number < password.length() && answer != "y"; number++)

The same problem appears in checkCharacterTwo(), that should become:

for (int number = 0; number < password.length() && answer != "y"; number++)

Finally, you don't set the value of capitalAnswerTwo. You have to replace:

 string checkCharacterTwo(password);

with:

 capitalAnswerTwo = checkCharacterTwo(password);

Then everything is fine !

Christophe
  • 68,716
  • 7
  • 72
  • 138