What could I have done better?

Below is the code that I submitted last night for one of my class projects. What could I have done different to make it cleaner?

Here's the prompt:

Project Six
Strings and Vectors
Write a C++ program that allows the user to input these string values:
1- User’s first name into firstName.
2- User’s last name into lastName.
3- User’s password into password
The program places the first letter of the first name plus the first 5 characters of the last name into the string userName. You can assume the last name is at least 5 characters.
The program then concatenates userName and password into a string variable login.
login takes the form userName, password (note the comma and space)
For example pmadis, abc123
The combined strings will then be stored in a string vector.
Before combining the strings, write a function named checkPassword to request the password and validate the entry. The function should verify the following:
o Length of the string is at least 5 characters.
Error message =: Password is not 5 characters.
o Cannot contain a space.
Error message =: Password cannot contain a space.
Place the error messages in a string array, and display the messages before requesting the password in checkPassword. Display the appropriate error message when you find an error.
Repeat the loop until the user enters a valid password.

The function should return the string to main after the entry is correct.
Repeat the process of entering user names and passwords until the user enters 0 for firstName.

Output the list to the screen by displaying the vector items (the logins) one per line after building the vector.

 Set up the string password = “abc123”; and test the vector code first.
 After the vector code in main works:
o Change string password = “abc123”; to string password;
o Add the checkPassword function.
o The call should look like this: password = checkPassword();
o The checkPassword function returns the password after it is validated. The getline for password should be in checkPassword.
o Use getline to input the user name and password. Here is the syntax:
getline(cin, s); s is declared as a string.


And here's the code I submitted:
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
// Description:		Password Database Program
// IDE used:		Visual Studio 2013 Express

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

int main( )
{
	
	string firstName, lastName, password, userName, login;
	vector<string> list;

	string checkPassword();

	firstName = "1";

	cout << "\n\n        USER LOGIN DATABASE";

	cout << "\n\n\n    Please enter the user's first name or 0 to exit:    ";
	getline(cin, firstName);

	while (firstName != "0")
	{

		cout << "\n\n    Please enter the user's last name:    ";
		getline(cin, lastName);

		lastName = lastName.substr(0,5);

		password = checkPassword();

		userName = firstName[0] + lastName;

		login = userName + ", " + password;
		
		list.push_back(login);

		cout << "\n    Please enter the user's first name or 0 to exit:    ";
		getline(cin, firstName);
	}
    
	cout << "\n\n    Login data entered:" << endl << endl;

	for (unsigned int n = 0; n < list.size(); ++n)
		cout << "         " << list.at(n) << endl << endl;
	
    cout << endl;

	system("pause");
    return 0;
}


string checkPassword()
{
	bool errorFlag = false;
	string userPassword;

	cout << "\n\n    Please enter the user's password:    ";
	getline(cin, userPassword);
	cout << endl << endl;

	do
	{
		int n = userPassword.length();
		if (n < 5)
		{
			cout << "\n\n\n\n    ERROR: The user's password is not long enough. \n           Please create a password that is at least 5 characters.\n\n\n\n";
			errorFlag = true;
			cout << "\n\n    Please enter the user's password:    ";
			getline(cin, userPassword);
			cout << endl << endl;
		}

		size_t found = userPassword.find(' ');
		if (found != string::npos)
		{
			cout << "\n\n\n\n    ERROR: The user's password cannot contain spaces. \n           Please create a password without spaces.\n\n\n\n";
			errorFlag = true;
			cout << "\n\n    Please enter the user's password:    ";
			getline(cin, userPassword);
			cout << endl << endl;
		}

	}
	while (errorFlag = false);

	string returnedPass[] = { userPassword };

	return(returnedPass[0]);
}
I won't look if your program works fine, I'll just look at the code.

(move the decleration 15: string checkPassword(); outside of main.) -opinion

17: firstName = "1"; not necessary

30: lastName = lastName.substr(0,5);
Why are you doing that?

34: userName = firstName[0] + lastName;Why write firstName[0] and not just firstName?
That makes it look like it's an array.

88: while (errorFlag = false);
you set the errorFlag here, this will never loop

1
2
3
90: string returnedPass[] = { userPassword };
91:
92: return(returnedPass[0]);
you make it look like an array but it's just a string, try to not do that.
17: firstName = "1";
not necessary

Ok.


30: lastName = lastName.substr(0,5);

Why are you doing that?

34: userName = firstName[0] + lastName;

Why write firstName[0] and not just firstName?
That makes it look like it's an array.

30 & 34: The prompt says that the username should be the first character of firstName and the first five characters of lastName. In 30 I intended to redefine lastName as the first five characters of what came in from getline(cin, lastName). In 34, I pulled the character in position 0 to get the first character.

For both, is a different (better) way to have coded it?


88: while (errorFlag = false);

you set the errorFlag here, this will never loop

Yeah, I can get it to cycle through one of each if statements but not a second time. I will have to look at that.



90: string returnedPass[] = { userPassword };
91:
92: return(returnedPass[0]);

you make it look like an array but it's just a string, try to not do that.

The reason I did this was because I read that you couldn't return a string out of a function directly.
Last edited on
30 & 34: The prompt says that the username should be the first character of firstName and the first five characters of lastName. In 30 I intended to redefine lastName as the first five characters of what came in from getline(cin, lastName). In 34, I pulled the character in position 0 to get the first character.

For both, is a different (better) way to have coded it?


Okey sorry, I didn't read the specs
Nah its fine, just go for it like this.

The reason I did this was because I read that you couldn't return a string out of a function directly.

That's just wrong, you can do it ! ;)
Topic archived. No new replies allowed.