How to Implement Rule

I have this project where we have to take a rule off of this website https://uic.edu/apps/strong-password/ and calculate the number of points for each Column and we have to do the first 5 of each section. I was able to do the number of characters one pretty easily but I was wondering if somebody could give me advice on how to do the one for uppercase letters. The code I have done so far is done below.

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
31
32
  #include <iostream>
#include <string>
using namespace std;

class Validatestring 
{
    public:
    string password; 
    int rate;
    int count;
    int bonus;
    virtual int GetCount()=0;
    virtual void CalculateRate()=0
};

class NumberOfCharacters : public Validatestring
{
    public:
    int GetCount();
    void CalculateRate();
};

int NumberOfCharacters::GetCount()
{
    return password.length();
}

void NumberOfCharacters::CalculateRate()
{
bonus = GetCount()*4;
}
Last edited on
on how to do the one for uppercase letters
Do you want to count the number of uppercase letters?

If yes lust loop through all the characters in the string and check if the character is usppercase https://www.cplusplus.com/reference/cctype/isupper/
or if allowed you could use std::count_if
https://www.cplusplus.com/reference/algorithm/count_if/
Well that website reference doesn't work - not found.
I will try the looping. Thank you for the suggestion.
Well that website reference doesn't work - not found.


The website link has been corrected and updated.
The link is still 404. This site's link processor get confused easily when adding extra characters to the end of a link.

Either forget enclosing a link in parentheses, OR use an empty set of format tags between the end of the link and the closing ).

Like this: (https://uic.edu/apps/strong-password/). {I used the teletype tags, no visual indication used}.
The link is still 404. This site's link processor get confused easily when adding extra characters to the end of a link.


It should be fixed now.
I can't get the main to run, any suggestions?

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <string>
using namespace std;

class Validatestring
{
public:
    string password;
    int rate;
    int count;
    int bonus;
    virtual int GetCount() = 0;
    virtual void CalculateRate() = 0;
};

class myabstractclass
{
public:
    string password;
    myabstractclass() {};
    void setpassword(string s) { password = s; };
};

class NumberOfCharacters : public Validatestring
{
public:
    int GetCount();
    void CalculateRate();
};

int NumberOfCharacters::GetCount()
{
    return password.length();
}

void NumberOfCharacters::CalculateRate()
{
    bonus = GetCount() * 4;
}


class UpperCase : public myabstractclass
{
public:
    int GetUpperCase()
    {
        int counter = 0;
        for (int i = 0; i < password.length(); i++)
            if (isupper(password[i]))
                counter++;
        return counter;
    }

    int GetBonus()
    {
        int x = GetUpperCase();
        return ((password.length() - x) * 2);
    }
};

void main()
{
    UpperCase U;
    U.password = "NickThompson";
    U.GetUpperCase();
    cout << U.GetBonus << endl;
}
L61: main must return int
L66: you forgot the ()
SMH. I've been trying to fix it for an hour.
Any ideas on how I could do letters only formula from this website https://uic.edu/apps/strong-password/

This is what I have so far:
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <string>
using namespace std;

class Validatestring
{
public:
    string password;
    int rate;
    int count;
    int bonus;
    virtual int GetCount() = 0;
    virtual void CalculateRate() = 0;
};

class myabstractclass
{
public:
    string password;
    myabstractclass() {};
    void setpassword(string s) { password = s; };
};

class NumberOfCharacters : public Validatestring
{
public:
    int GetCount();
    void CalculateRate();
};

int NumberOfCharacters::GetCount()
{
    return password.length();
}

void NumberOfCharacters::CalculateRate()
{
    bonus = GetCount() * 4;
}


class UpperCase : public myabstractclass
{
public:
    int GetUpperCase()
    {
        int counter = 0;
        for (int i = 0; i < password.length(); i++)
            if (isupper(password[i]))
                counter++;
        return counter;
    }

    int GetUpperBonus()
    {
        int x = GetUpperCase();
        return ((password.length() - x) * 2);
    }
};

class LowerCase : public myabstractclass
{
public:
    int GetLowerCase()
    {
        int counter = 0;
        for (int i = 0; i < password.length(); i++)
            if (islower(password[i]))
                counter++;
        return counter;
    }

    int GetLowerBonus()
    {
        int x = GetLowerCase();
        return ((password.length() - x) * 2);
    }
};

class Numbers : public myabstractclass
{
public:
    int GetNumbers()
    {
        int counter = 0;
        for (int i = 0; i < password.length(); i++)
            if (isdigit(password[i]))
                counter++;
        return counter;
    }

    int GetNumberBonus()
    {
        int x = GetNumbers();
        return (x * 4);
    }
};

class Symbols : public myabstractclass
{
public:
    int GetSymbols()
    {
        int counter = 0;
        for (int i = 0; i < password.length(); i++)
            if (ispunct(password[i]))
                counter++;
        return counter;
    }

    int GetSymbolBonus()
    {
        int x = GetSymbols();
        return (x * 6);
    }
};

Last edited on
I could do letters only formula


For info re the various isxxx(), see https://en.cppreference.com/w/cpp/string/byte

isalpha() seems to be what is required.

Note that you can use range-for. Instead of say:

1
2
3
4
5
6
7
8
int GetUpperCase()
    {
        int counter = 0;
        for (int i = 0; i < password.length(); i++)
            if (isupper(password[i]))
                counter++;
        return counter;
    }


You could have:

1
2
3
4
5
6
7
8
size_t GetUpperCase() {
	size_t counter {};

	for (const unsigned char ch : password)
		counter += isupper(ch);

	return counter;
}


Note that .length() returns a type size_t and not a type int.
I tried your recommendation by doing the code below, but it's still not quite coming out quite right. I got -2 instead of 0. Any recommendations or solutions? Do y'all think I need an if else statement?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class LettersOnly : public myabstractclass
{
public:
size_t GetLettersOnly() {
    size_t counter{};

    for (const unsigned char ch : password)
        counter += isupper(ch);

    return counter;
}

int GetLettersOnlyBonus()
    {
        int x = GetLettersOnly();
        return -(x);
    }
};
Last edited on
cppreference wrote:
isupper returns a nonzero value only for the uppercase letters (ABCDEFGHIJKLMNOPQRSTUVWXYZ).

https://en.cppreference.com/w/cpp/string/byte

"Nonzero" does not mean "one".
1
2
3
4
5
6
7
8
size_t GetLettersOnly() {
    size_t counter{};

    for (const unsigned char ch : password)
        counter += isupper(ch) == 0? 0: 1;

    return counter;
}


You may prefer to use the built-in operator! twice as follows
1
2
3
4
5
6
7
8
size_t GetLettersOnly() {
    size_t counter{};

    for (const unsigned char ch : password)
        counter += !!isupper(ch);

    return counter;
}

The effects are the same.
I am not quite sure what you mean. Would you be able to show me how I should put it into the code?
I am not quite sure what you mean. Would you be able to show me how I should put it into the code?

No. I have shown enough already.
Is this what you meant? I have it working but it is still bringing back a value of -2 instead of 0.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class LettersOnly : public myabstractclass
{
public:
    int GetLettersOnly()
    {
         size_t GetLettersOnly();
         {
            size_t counter{};

            for (const unsigned char ch : password)
                counter += isupper(ch) == 0 ? 0 : 1;

            return counter;
        }
    }

    int GetLettersOnlyBonus()
    {
        int x = GetLettersOnly();
        return (- (x));
    }
};
Last edited on
Thanks for the help guys! I didn't figure it out but I appreciate the help anyways!
For VS2022:

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

size_t GetUpperOnly(const std::string& password) {
	size_t counter {};

	for (const unsigned char ch : password)
		counter += std::isupper(ch);

	return counter;
}

int main() {
	const std::string pass {"aBcDeF"};

	std::cout << GetUpperOnly(pass) << '\n';
}


Displays:


3



If your compiler doesn't return 1 or 0 for isxxx(), then generalised:

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

size_t GetUpperOnly(const std::string& password) {
	size_t counter {};

	for (const unsigned char ch : password)
		counter += std::isupper(ch) ? 1 : 0;

	return counter;
}

int main() {
	const std::string pass {"aBcDeF"};

	std::cout << GetUpperOnly(pass) << '\n';
}


For just alpha chars, then:

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

size_t GetLettersOnly(const std::string& password) {
	size_t counter {};

	for (const unsigned char ch : password)
		counter += std::isalpha(ch) ? 1 : 0;

	return counter;
}

int main() {
	const std::string pass {"(B!c$eF"};

	std::cout << GetLettersOnly(pass) << '\n';
}


Which displays:


4

Last edited on
Topic archived. No new replies allowed.