Assignment question

Hey guys, this is my first time posting something on here and I could really use your help. I have a question for class and I have no idea where on earth to even start. That being said, I do not have a code yet. I was hoping you guys could help me, it would be greatly appreciated.

------Write a C++ program to setup and test a three level set of passwords. The first level is a password that contains at least one uppercase letter, one lowercase letter, at least one digit, and at least 6 letters. The second level is a sentence. The third level is a question-answer password.-------

This is my first class on programming and I have no idea. Please help me!!!!!
You can use isalpha, isdigit and length for the first part.

For the other two parts there's not a lot of information, so I'll assume you have to use getline(cin, password)
Hello alarue1s,

Since you have nothing to work with yet start off in small pieces. This is my first quick thoughts.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <cctype>

bool CheckPasswordL1(std::string str);

int main()
{
// ********** PART1 **********
	// Get level from user.
	// Get input from user

// ********** PART2 **********
	// Call function to check password.

	
	return 0;
}

bool CheckPasswordL1(std::string str)
{

}

Start with part 1 and get it working then you can move on to part 2 then to part 3 and for and so on. This way the next part builds of the last.

Hope that helps,

Andy
@HandyAndy, isn't it better to pass the password string as reference to const to avoid unnecessary copies?

 
bool CheckPasswordL1(const std::string& str);
@boring,

Yes you are right. It is a new concept that I recently read about here, passing by reference, and I am not quite use to using it yet. Missed the "const", but I did say it was a quick thought.

Thanks for the catch.

Andy
Last edited on
@boring
@HandyAndy

Ok, this is what I have come up with, but I don't know if this is the correct way to do it and I dont know how to test the sentence or the question answer levels for the password.

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

const int PASS_SIZE = 6;
bool testPass(string pass);

int main()
{
string L1Pass, L2Pass, L3Pass1, L3Pass2, inputPass1, inputPass2, inputPass3;
cout << "Stage 1: set up your level 3 setup password" << endl;
cout << "For level 1 please enter a password that contains at least one uppercase letter, ";
cout << "one lowercase letter, at least one digit and at least 6 digits long." << endl;
cin >> L1Pass;

cout << "For level 2 please enter a password in the form of a sentence that has a total of, ";
cout << "at least three words ending in a period." << endl;
cin >> L2Pass;

cout << "For level three, please enter a question-answer password" << endl;
cin >> L3Pass1, L3Pass2;

while (!testPass(L1Pass))
{
cout << "Your password is not valid. Please reenter it" << endl;
cout << "For level 1 please enter a password that contains at least one uppercase lette,r";
cout << "one lowercase letter, at least one digit and at least 6 digits long." << endl;
cin >> L1Pass;
}
while (!testPass(L2Pass))
{
cout << "Your password is not valid. Please reenter it" << endl;
cout << "For level 2 please enter a password in the form of a sentence that has a total of";
cout << "three words." << endl;
cin >> L2Pass;
}
while (!testPass(L3Pass1))
{
cout << "Your password is not valid. Please reenter it" << endl;
cout << "For level three, please enter a question-answer password" << endl;
cin >> L3Pass1;
}
while (!testPass(L3Pass1))
{
cout << "Your password is not valid. Please reenter it" << endl;
cout << "For level three, please enter a question-answer password" << endl;
cin >> L3Pass2;
}
cout << "Stage 2: logging in using your password" << endl;
int counter = 1;
while (counter <= 3)
{
cout << "Please enter your level one password" << endl;
cin >> inputPass1;
if (inputPass1 == L1Pass)
{
cout << "Correct password. You are logged in" << endl;
counter = 100;
}
else
{
cout << "Your password is not correct." << endl;
cout << "Your account will be locked after 3 failed trials" << endl;
counter++;
if (counter > 3)
{
cout << "Your account will be locked" << endl;
}
}
}
system("pause");
return 0;
}
/* This function will check input and ensure that the password's size is PASS_SIZE or
greater and contains a uppercase, lowercase, and digit.*/
bool testPass(string pass)
{
int length = pass.size();
//flags
bool aUpper = false,
aLower = false,
aDigit = false,
aSize = false;
for (int i = 0; pass[i]; ++i)
if (isupper(pass[i]))
aUpper = true;
else if (islower(pass[i]))
aLower = true;
else if (isdigit(pass[i]))
aDigit = true;
if (length >= PASS_SIZE)
cout << "Your password is not the correct size." << endl;
else
aSize = true;
if (aUpper == false)
cout << "Your password does not contain an uppercase letter." << endl;
if (aLower == false)
cout << "Your password does not contain a lowercase letter." << endl;
if (aDigit == false)
cout << "Your password does not contain a digit" << endl;
if (aUpper && aLower && aDigit && aSize)
return true;
else
return false;
}[/code]
@alarue1s, I would ask your teacher for more information about the requirements. What exactly needs to be done with levels two and three?

Is "hi hi" a Level-2 password because it has a space and is therefore a sentence?
What is a question-answer password, anyway -- is it a single word, or is it three pieces of information (password, secret_question, secret_answer) ?

Just too much open to interpretation. Your approach might be correct, but it's hard to tell. [ code ] tag (opening tag) missing in your last post, btw.
Hello alarue1s,

It looks like you may have missed your opening code tag. It happens.

Since L1Pass should be a single word the formatted "cin >>" works. Using "cin" this way you will need to follow it with:
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>. to clear the input buffer of the new line character.

The input for L2Pass is to be a sentence, but using the formatted cin only extracts the first word up to the first white space and leaves the rest in the input buffer for whatever comes next to read. Because there are spaces in the sentence you will need to use "std::getline()" to extract the entire sentence including the new line.

Instead of waiting you should be testing the entry of the "L1Pass" with a call to "testPass()". This way if there is an error with the entered password you can fix it right then. Move the while loop up to between the "cin >>" and the "std::cin.ignore".

For testing I would comment everything that applies to L2 and L3 passwords until I have L1 working then move on to L2.

I have not checked into the "testPass" function completely, But I noticed the for loop will not work. I believe what you want here is: for (int i = 0; i < pass.size(); ++i).

I will dig into the program shortly and let you know what I find.

Hope that helps,

Andy
Hello alarue1s,

Went to work on just the level 1 password first.

Moved the while loop up as mentioned earlier. It now look like this:
1
2
3
4
5
6
7
8
9
10
11
12
        // <--- Level 1 instructions.
	cin >> L1Pass;

	while (!testPass(L1Pass))
	{
		cout << "Your password is not valid. Please reenter it" << endl;
		cout << "For level 1 please enter a password that contains at least one uppercase lette,r";
		cout << "one lowercase letter, at least one digit and at least 6 digits long." << endl;
		cin >> L1Pass;
	}

	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>. 


In the "testPass" function I rearranged things a bit along with a couple of additions that the comments will explain;
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
bool testPass(string pass)
{
	int length = pass.size();
	//flags
	bool aUpper = false,
		aLower = false,
		aDigit = false,
		aSize = false;

	if (length < PASS_SIZE)  // <--- Check this first. If less than 6 there is no need to continue.
	{
		cout << "Your password is not the correct size." << endl;
		return false;
	}
	else
		aSize = true;

	for (int i = 0; i < pass.size(); ++i)
	{
		if (isupper(pass[i]))
			aUpper = true;
		else if (islower(pass[i]))
			aLower = true;
		else if (isdigit(pass[i]))
			aDigit = true;

		if (aUpper && aLower && aDigit) break;  // <--- Once all is true no need to continue.
	}

	if (aUpper == false)  // <--- Alternative " if (!aUpper) works the same.
		cout << "Your password does not contain an uppercase letter." << endl;

	if (aLower == false)
		cout << "Your password does not contain a lowercase letter." << endl;

	if (aDigit == false)
		cout << "Your password does not contain a digit" << endl;

	if (aUpper && aLower && aDigit && aSize)
		return true;
	else
		return false;
}


I added the {}s in the for loop at first because I thought they were need until I realize that the if/else if statements did not need the {}s, but in the end I added a line, so everything worked out.

When you enter something for "L2Pass" you will need to call a different function from "testPass" because now you have to check for three words in the password.

I am thinking along the lines of put "L2Pass" in a string stream and use a while loop to count the number of words. If the count is greater than 2 call the "testPass" function. If everything is acceptable return true else return false.

When this is done then move on to level 3.

Hope that helps,

Andy
Topic archived. No new replies allowed.