patterncppModerate
Basic password authentication system app
Viewed 0 times
appsystemauthenticationpasswordbasic
Problem
This is a console app I wrote for an end-of-chapter assignment in Jumping into C++. The parameters were for it to have a basic password system, and if you are eager, a multiple password / username system. I am going through this book completely on my own free will. It's not through a school assignment or whatnot.
It works quite well, but as I have a limited programming background, I wanted to post it here so any bad techniques or poor syntax could be brought to my attention.
It works quite well, but as I have a limited programming background, I wanted to post it here so any bad techniques or poor syntax could be brought to my attention.
#include
using namespace std;
string usrnam[] = { "admin", "user", "" };
string pasword[] = { "root", "default", ""};
string temp_pass;
string temp_usr;
bool loggedin;
int n = sizeof(usrnam) / sizeof(string);
int findPassword( string str[], int array_size, string query )
{
for ( int i = 0; i > stage;
switch(stage)
{
case 1: {
cout > answer;
if (answer == "y" || answer == "Y")
{
cout > usrnam[n-1];
cout > pasword[n-1];
login(usrnam[n-1],pasword[n-1]);
}
}
int main()
{
cout > temp_usr;
cout > temp_pass;
login(temp_usr,temp_pass);
if(!loggedin)
registerUser();
}Solution
-
Try using
Something like this:
Then, the size of the
-
Try to use proper names as variables. Avoid wrong spellings or short-names. Exact variable names make the code easy to understand, as the name represents the actual concept the variable is referring to.
-
Also, in
Like:
-
In your code, the method
-
Also, the
Try using
std::vector instead of arrays. As a result, you'll not be required to keep track of the array_size in a separate variable like n in your case.Something like this:
std::vector username;
username.push_back("admin");
//other entries go here
std::vector password;
password.push_back("root");
//other entries go hereThen, the size of the
std::vector is given by size().-
Try to use proper names as variables. Avoid wrong spellings or short-names. Exact variable names make the code easy to understand, as the name represents the actual concept the variable is referring to.
-
Also, in
registerUser() the variable answer can just be a char. This will help in saving a little memory.Like:
char answer;
std::cin>>answer;
if(answer=='y' || answer=='Y')
{
//code goes here
}-
In your code, the method
commandC() is defined to return an int. But it doesn't do so. Make the return type void instead.void CommandC()
{
//code goes here
}-
Also, the
main() method is defined to return an int too. It is a good practice to declare it like that, but make sure to return an integer. 0 (zero) is preferred as it refers to successful completion of main().int main()
{
//code goes here
return 0; //indicates successful termination
}Code Snippets
std::vector<std::string> username;
username.push_back("admin");
//other entries go here
std::vector<std::string> password;
password.push_back("root");
//other entries go herechar answer;
std::cin>>answer;
if(answer=='y' || answer=='Y')
{
//code goes here
}void CommandC()
{
//code goes here
}int main()
{
//code goes here
return 0; //indicates successful termination
}Context
StackExchange Code Review Q#37235, answer score: 14
Revisions (0)
No revisions yet.