C++Noob problem

closed account (9G8MoG1T)
Hey guys I have been at this for a while and I keep running into trouble on verifying if a number is contained within a given string.
Could you guys please kick my butt in the right direction or hint.

#include <iostream>
#include <string>

using namespace std;

int validation(string password)
{
if (password.size() < 7)
{
return 2;
}

char characters;
int valid = 0;
for (int i = 0; i < password.size(); ++i)
{
characters = password[i];
if (toupper(characters) == password[i])
{
valid = 1;
}
}
if (valid == 0)
{
return 3;
}

valid = 0;
///****THIS IS WHERE I AM TRYING TO INPUT A VALIDATION FOR THE NUMBER******

return 1;
}

int main()
{
string password;
while (true)
cout << "Please enter a password: ";
cin >> password;
if (validation(password) == 1)
{
cout << "Thank you, that is a valid passowrd." << endl << endl;
}
else if (validation(password) == 2)
{
cout << "ERROR: Password must be at least 7 characters long. Please, try again." << endl << endl;
}
else if (validation(password) == 3)
{
cout << "ERROR: Password must include at least one uppercase letter. Please, try again." << endl << endl;
}
else if (validation(password) == 4)
{
cout << "ERROR: Password must contain at lease one number. Please, try again." << endl << endl;
}
return 0;
}
Here is how I would try and find a digit in string. Let me know if you have any questions.

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
#include <iostream>

using namespace std;

int main (){
// get user input
    string user_input;
    cout << "Please enter a password"  << endl;
    cin >> user_input;

// loop over string char by char and keep count of loop
    int str_pos = 0;
    for(char& c : user_input) {

// if a digit do things..
        if(isdigit (c)){

// printing out to prove it works but this might be weird if you have more than one digit
            cout << "There is a digit in your password" << endl;
            cout << "It is located in postion [" << str_pos <<"] of your input" << endl;
        }
        str_pos++;
    }

return 0;
}
Last edited on
You dont need to run another loop for checking for digit, use same loop where you are checking toupper

modified code
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
int validation(string password)
{
        if (password.size() < 7)
        {
                return 2;
        }

        char characters;
        int valid = 0;
        for (int i = 0; i < password.size(); ++i)
        {
                characters = password[i];
                if (toupper(characters) == password[i])
                {
                        valid = 1;
                }
                else if(isdigit(password[i]))
                {
                        //Your logic of validation of digit in the string
                }
        }

        if (valid == 0)
        {
                return 3;
        }

        return 1;
}


C++11 style code
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
int vvalidation(string password)
{
        if (password.size() < 7)
        {
                return 2;
        }

        bool valid = false;
        for (auto c: password)
        {
                if (toupper(c) == c)
                {
                        valid = 1;
                }else if(isdigit(c))
                {
                        //Your logic of validation of digit in the string
                }

        }
        if (valid == 0)
        {
                return 3;
        }

        return 1;
}


You need to add your validation logic in place of the comment
closed account (9G8MoG1T)
THANKS GUYS! You guys were of great help! Life savers!
Topic archived. No new replies allowed.