assignment help

have to do this assignment and my professor does not reply at the moment and my assignment is due to 15th of december. Really need help with this program since you cant find anything online wihtout using cctype library or some other stuff that i havent heard of. i am studying c++ for 2 months and so far we have covered if else,loops,srand and a little bit of functions and thats it.

Write a program that generates random passwords based on the following rules:
a. The password length is 8 characters.
b. The password must contain one upper-case letter.
c. The password must contain one digit (0-9).
d. The password must have one of the following special characters: $, *, ^, &, #, _,
?.
e. The upper-case letter, digit and special character must not always be placed in the
same place in the password.
Note: you cannot use the cctype library
This is a variation of this post http://www.cplusplus.com/forum/beginner/274833/

This asks for one of each, the other asks for at least one.

e) ???

So for a variation on the other 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
30
31
#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main()
{
	const char* const punct {"$*^&#_?."};
	bool good {};

	do {
		string pass;

		cout << "Enter a strong password: ";
		cin >> pass;

		if ((good = pass.size() == 8)) {
			size_t up {};
			size_t dig {};
			size_t pun {};

			for (char ch : pass) {
				up += (ch >= 'A' && ch <= 'Z');
				dig += (ch >= '0' && ch <= '9');
				pun += strchr(punct, ch) != NULL;
			}

			good &= up == 1 && dig == 1 && pun == 1;
		}
	} while (cout << (good ? "Password OK\n" : "Bad password\n") && !good);
}



they work,thank you for taking your time but we havent went through cstring library and i want to understand the code and not just copy paste. Is ther a way to do it without cstring library?
Yes.

pun += ch == '$' || ch == '*' || ch == '&' ..... etc etc


or you can use string instead. Replace the appropriate lines with:

1
2
3
const string punct {"$*^&#_?."};
...
pun += punct.find(ch) != string::npos;

Last edited on
so size_t dig,pun and extc is basically like int pun,dig am i right? and could you pls explain the "for" usage. I am used to for loop looking like this >>> for (int i = 0; i <= n; i++) but (char ch : pass) is something new that i cannot understand
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
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <random>
using namespace std;

const string Upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string Lower = "abcdefghijklmnopqrstuvwxyz";
const string Digit = "0123456789";
const string Special = "$*^&#_?";
const string All = Upper + Lower + Digit + Special;


string password()
{
   string result;
   result = result + Upper[rand()%Upper.size()] + Digit[rand()%Digit.size()] + Special[rand()%Special.size()];
   while ( result.size() < 8 ) result += All[rand()%All.size()];
   random_shuffle( result.begin(), result.end() );
   return result;
}


bool checkPassword( const string &pass )
{
   return pass.size() == 8 
       && pass.find_first_of( Upper ) != string::npos
       && pass.find_first_of( Digit ) != string::npos
       && pass.find_first_of( Special ) != string::npos
       && pass.find_first_not_of( All ) == string::npos;
}


int main()
{
   string pass;
   srand( time(0) );
   cout << "Suggested password: " << password() << '\n';
   cout << "Enter your own password: ";   getline( cin, pass );
   cout << pass << " " << ( checkPassword( pass ) ? "is" : "is not" ) << " OK\n";
}

Last edited on
 
for (char ch : pass)


This is a range-based for loop. The container (string pass in this case) is iterated over its contents from beginning to and and ch becomes the individual element in each iteration. The for loop will terminate after the last element has been used.

So :

1
2
3
4
const string pass {"qwert"};

for (char ch : pass)
    cout << ch;


ch will be first q then w then e etc so the display will be qwert

See https://en.cppreference.com/w/cpp/language/range-for
so size_t dig,pun and extc is basically like int pun,dig am i right?
Yes. size_t is usually defined to be an unsigned int :

EDIT: See jlb's explanation below. Also:

https://en.cppreference.com/w/cpp/types/size_t

Last edited on
Hello akira5555,

Trying to keep it simple based on what you have learned consider this:
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
#include <iostream>
#include<string>

using namespace std;

int main()
{
    string pass{ "Asd&f1ghiJ2 qweR$" }; // <--- Used for testing. Remove the {}s and everything between when finished.
    short uppercase{}, number{}, punctuation{};  // <--- Always a good idea to initialize your variables.

    //cout << "Enter a strong password: ";
    //cin >> pass;
    cout << "Enter a strong password: " << pass << '\n'; // <--- Used for testing. Comment or remove when finished.
    
    if (pass.size() < 8)
    {
        std::cerr << "\n Password to short! Must be at least 8 characters.\n\n";
    }

    if (pass.size() >32)
    {
        std::cerr << "\n Password to long! Must be no more than 32 characters.\n\n";
    }

    for (size_t idx = 0; idx < pass.size(); idx++)
    {
        uppercase += (pass[idx] >= 'A' && pass[idx] <= 'Z');
        number += (pass[idx] >= '0' && pass[idx] <= '9');
        
        if (pass[idx]== '$' || pass[idx]== '*' || pass[idx] == '^' || pass[idx] == '&' || pass[idx] == '#'
            || pass[idx] == '_' || pass[idx] == '?' || pass[idx] == ' ' || pass[idx] == '.')
        {
            punctuation ++;

        }

        //std::cout << '\n'; // <--- Used for testing. Comment or remove when finished.
    }

    if (uppercase && number && punctuation)
    {
        std::cout << "\n   Password is good\n\n";
    }
    else
    {
        std::cout << "\n     Password is not correct!\n";
    }

    return 0;
}

This is not complete. You still need a while or do/while loop and maybe some adjustment to what is there.

Point (e) I have a problem with.

The upper-case letter, digit and special character must not always be placed in the
same place
in the password.


If it is the first time the program is run how would you know if what is entered is in the same place? And after that why would it make any difference?

Line 30/31 can be adjusted if I have misunderstood and of the punctuation characters are incorrect or you need to add more.

Andy
Yes. size_t is usually defined to be an unsigned int :

No, size_t is an implementation defined unsigned type. On 32 bit systems it may normally be an unsigned int, however on 64 bit systems it is usually an unsigned long. The important part is that it " can store the maximum size of a theoretically possible object of any type (including array)".
@jlb Thanks for the correction.
Topic archived. No new replies allowed.