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
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?
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
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.
#include <iostream>
#include<string>
usingnamespace 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.
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)".