Post: C++ Login Code
02-03-2011, 05:21 AM #1
Ritztro
I am a Game Developer
(adsbygoogle = window.adsbygoogle || []).push({}); Well I was thinking about making something like this, so I did. It uses maps to store the username and pass. You can view how here. You can also write the maps to a file if you want and check against the file...

Code:
    #include <cstdlib>
#include <iostream>
#include <string>
#include <map>

using namespace std;

int main()
{
string pass;
string username;
bool admin;
bool blogin = false;

//Hack protection, because blogin shouldnt = true yet. Now if you call a function that does this and u use it multiple times, dont do this.
if(blogin == true || admin == true)
{
return 0;
}

map <string,string> login;
map <string,string> adminlogin;

adminlogin["Dutch"] = "password"; // Dutch (the username) is the name of the map element and "password" is the actual string for the password.
while(blogin == false)
{
cout << "Please enter your username" << endl;
cin >> username;
cout << "Please enter you pass" << endl;
cin >> pass;

if(pass == adminlogin[username])
{
cout << "Welcome " << username << " You are an admin" << endl;
admin = true; // YOu can give them special permissions if this is true..
blogin = true;
}

else if(pass == login[username])
{
cout << "Welcome " << username << endl;
blogin = true;
}

else
{
cout << "Login Failed please try again" << endl;
system("cls");
}

}

return 0;
}
Last edited by Ritztro ; 02-03-2011 at 05:24 AM.
02-03-2011, 07:31 AM #2
Originally posted by Dutch View Post
Well I was thinking about making something like this, so I did. It uses maps to store the username and pass. You can view how here. You can also write the maps to a file if you want and check against the file...

Code:
    #include <cstdlib>
#include <iostream>
#include <string>
#include <map>

using namespace std;

int main()
{
string pass;
string username;
bool admin;
bool blogin = false;

//Hack protection, because blogin shouldnt = true yet. Now if you call a function that does this and u use it multiple times, dont do this.
if(blogin == true || admin == true)
{
return 0;
}

map <string,string> login;
map <string,string> adminlogin;

adminlogin["Dutch"] = "password"; // Dutch (the username) is the name of the map element and "password" is the actual string for the password.
while(blogin == false)
{
cout << "Please enter your username" << endl;
cin >> username;
cout << "Please enter you pass" << endl;
cin >> pass;

if(pass == adminlogin[username])
{
cout << "Welcome " << username << " You are an admin" << endl;
admin = true; // YOu can give them special permissions if this is true..
blogin = true;
}

else if(pass == login[username])
{
cout << "Welcome " << username << endl;
blogin = true;
}

else
{
cout << "Login Failed please try again" << endl;
system("cls");
}

}

return 0;
}


The user can just press the cross in the top right :P, or Alt + F4. I've tried to make something similar to this but more complicated but never finished it, ill see if i can did out the source code :P
02-03-2011, 11:08 PM #3
Ritztro
I am a Game Developer
Originally posted by TheUberFail View Post
The user can just press the cross in the top right :P, or Alt + F4. I've tried to make something similar to this but more complicated but never finished it, ill see if i can did out the source code :P


What? The point isnt to exit lol. Its to login. Exiting comes once the program ends. You can add more code to it if you want...

You have a lot to learn about c++ dude. I can help you if u want.
Last edited by Ritztro ; 02-03-2011 at 11:19 PM.
02-04-2011, 10:18 AM #4
Sorry i was under the impression this was supposed to lock the computer, as in they have to log back in. and i did not mean, i was going to improve your code, i meant i attempted something simular to this and never finished it, Just been working on it now as well

here

    

#include<iostream>
#include <string>
#include "drawMenu.h"

using namespace std;

void handelDisplay();

void accessDenied()
{
system("cls");
cout<<"\a\n\tAccess Denied, You need level 1 clearence!";
Sleep(3500);
return;
}

class CDetails
{
public:
string username;
string password;
short accessLevel;
CDetails() {};
CDetails(string setUser, string setPass, bool setAccess);
void displaySlot();
void getDetails();
bool checkDetails(string checkUser, string checkPass);
};

CDetails *slot_a = new CDetails ( "slot 1" , "default" , 0 );
CDetails *slot_b = new CDetails ( "slot 2" , "default" , 0 );
CDetails *slot_c = new CDetails ( "slot 3" , "default" , 1 );

void CDetails::getDetails()
{
if(accessLevel)
{
accessDenied();
return;
}
system("cls");
cout<<"\n\tEnter new username: ";getline(cin,username);
cout<<"\tEnter new password: ";getline(cin,password);
return;
}

CDetails::CDetails(string setUser, string setPass, bool setAccess)
{
username = setUser;
password = setPass;
accessLevel = setAccess;
}

void CDetails::displaySlot()
{
system("cls");
if(accessLevel)
{
accessDenied();
return;
}
cout<<"\n\tUsername: "<<username<<endl;
cout<<"\tPassword: "<<password<<endl;
cout<<"\t"; system("pause");
return;
}

bool CDetails::checkDetails(string checkUser, string checkPass)
{
if(checkUser == username && checkPass == password)
{
if(accessLevel)
{
accessDenied();
return false;
}
return true;
}
else
return false;
}

bool login()
{
string tempUser, tempPass;

system("cls");

cout<<"\n\tEnter username: "; getline(cin,tempUser);
cout<<"\tEnter password: "; getline(cin,tempPass);

system("cls");

if(slot_a->checkDetails(tempUser,tempPass))
return true;
else if (slot_b->checkDetails(tempUser,tempPass))
return true;
else if (slot_c->checkDetails(tempUser,tempPass))
return true;
else
return false;
}

void handelLogin()
{
if(login())
{
cout<<"\n\tYou have logged in successfully!";
}

else
{
cout<<"\n\t\aInccorect log in!";
}

Sleep(2500);
return;
}

void setDetails()
{
system("cls");
string names;

for(;Winky Winky
{
names="";
names += slot_a->username;
names += "|";
names += slot_b->username;
names += "|";
names += slot_c->username;
names += "|";
names += "Return";

system("cls");
cout<<endl;
switch(drawMenu("Set login details",names,1))
{
case 1: slot_a->getDetails();break;
case 2: slot_b->getDetails();break;
case 3: slot_c->getDetails();break;
case 4: return;
}
}
}

int main()
{
bool quit = 0;

for(;Winky Winky
{
system("cls");
cout<<endl;

switch(drawMenu("Welcome to my log in console","Login|Check Details|Set details|Exit",1))
{
case 1: handelLogin();break;
case 2: handelDisplay();break;
case 3: setDetails();break;
case 4: quit = 1; break;
}

if(quit)
break;
}

delete &slot_a;
delete &slot_b;
delete &slot_c;

return 0;
}

void handelDisplay()
{
system("cls");

string names;

names += slot_a->username;
names += "|";
names += slot_b->username;
names += "|";
names += slot_c->username;
names += "|";
names += "Return";

for(;Winky Winky
{
system("cls");
cout<<endl;
switch(drawMenu("Check login details",names,1))
{
case 1: slot_a->displaySlot();break;
case 2: slot_b->displaySlot();break;
case 3: slot_c->displaySlot();break;
case 4: return;
}
}
}

02-04-2011, 03:28 PM #5
Ritztro
I am a Game Developer
Originally posted by TheUberFail View Post
Sorry i was under the impression this was supposed to lock the computer, as in they have to log back in. and i did not mean, i was going to improve your code, i meant i attempted something simular to this and never finished it, Just been working on it now as well

here

    

#include<iostream>
#include <string>
#include "drawMenu.h"

using namespace std;

void handelDisplay();

void accessDenied()
{
system("cls");
cout<<"\a\n\tAccess Denied, You need level 1 clearence!";
Sleep(3500);
return;
}

class CDetails
{
public:
string username;
string password;
short accessLevel;
CDetails() {};
CDetails(string setUser, string setPass, bool setAccess);
void displaySlot();
void getDetails();
bool checkDetails(string checkUser, string checkPass);
};

CDetails *slot_a = new CDetails ( "slot 1" , "default" , 0 );
CDetails *slot_b = new CDetails ( "slot 2" , "default" , 0 );
CDetails *slot_c = new CDetails ( "slot 3" , "default" , 1 );

void CDetails::getDetails()
{
if(accessLevel)
{
accessDenied();
return;
}
system("cls");
cout<<"\n\tEnter new username: ";getline(cin,username);
cout<<"\tEnter new password: ";getline(cin,password);
return;
}

CDetails::CDetails(string setUser, string setPass, bool setAccess)
{
username = setUser;
password = setPass;
accessLevel = setAccess;
}

void CDetails::displaySlot()
{
system("cls");
if(accessLevel)
{
accessDenied();
return;
}
cout<<"\n\tUsername: "<<username<<endl;
cout<<"\tPassword: "<<password<<endl;
cout<<"\t"; system("pause");
return;
}

bool CDetails::checkDetails(string checkUser, string checkPass)
{
if(checkUser == username && checkPass == password)
{
if(accessLevel)
{
accessDenied();
return false;
}
return true;
}
else
return false;
}

bool login()
{
string tempUser, tempPass;

system("cls");

cout<<"\n\tEnter username: "; getline(cin,tempUser);
cout<<"\tEnter password: "; getline(cin,tempPass);

system("cls");

if(slot_a->checkDetails(tempUser,tempPass))
return true;
else if (slot_b->checkDetails(tempUser,tempPass))
return true;
else if (slot_c->checkDetails(tempUser,tempPass))
return true;
else
return false;
}

void handelLogin()
{
if(login())
{
cout<<"\n\tYou have logged in successfully!";
}

else
{
cout<<"\n\t\aInccorect log in!";
}

Sleep(2500);
return;
}

void setDetails()
{
system("cls");
string names;

for(;Winky Winky
{
names="";
names += slot_a->username;
names += "|";
names += slot_b->username;
names += "|";
names += slot_c->username;
names += "|";
names += "Return";

system("cls");
cout<<endl;
switch(drawMenu("Set login details",names,1))
{
case 1: slot_a->getDetails();break;
case 2: slot_b->getDetails();break;
case 3: slot_c->getDetails();break;
case 4: return;
}
}
}

int main()
{
bool quit = 0;

for(;Winky Winky
{
system("cls");
cout<<endl;

switch(drawMenu("Welcome to my log in console","Login|Check Details|Set details|Exit",1))
{
case 1: handelLogin();break;
case 2: handelDisplay();break;
case 3: setDetails();break;
case 4: quit = 1; break;
}

if(quit)
break;
}

delete &slot_a;
delete &slot_b;
delete &slot_c;

return 0;
}

void handelDisplay()
{
system("cls");

string names;

names += slot_a->username;
names += "|";
names += slot_b->username;
names += "|";
names += slot_c->username;
names += "|";
names += "Return";

for(;Winky Winky
{
system("cls");
cout<<endl;
switch(drawMenu("Check login details",names,1))
{
case 1: slot_a->displaySlot();break;
case 2: slot_b->displaySlot();break;
case 3: slot_c->displaySlot();break;
case 4: return;
}
}
}



Hey forget that I said that you have a lot to learn. You seem to know your c++ pretty well. Now all you have to do is learn sfml and then learn opengl with me so we can make a game!
02-04-2011, 05:09 PM #6
Originally posted by Dutch View Post
Hey forget that I said that you have a lot to learn. You seem to know your c++ pretty well. Now all you have to do is learn sfml and then learn opengl with me so we can make a game!


SFML looks very interesting, already downloaded the library and tutorial, going to test some basics out tonight, as for openGL, it looks horrible yet efficient :P
02-04-2011, 11:04 PM #7
Ritztro
I am a Game Developer
Originally posted by TheUberFail View Post
SFML looks very interesting, already downloaded the library and tutorial, going to test some basics out tonight, as for openGL, it looks horrible yet efficient :P


Oh ya its super ahrd to understand but its cababilities are amazing! Pretty much all games for PC, Linux, Mac and Ps3 use OpenGL
02-04-2011, 11:12 PM #8
Originally posted by Dutch View Post
Oh ya its super ahrd to understand but its cababilities are amazing! Pretty much all games for PC, Linux, Mac and Ps3 use OpenGL


Most modern games use DirectX I think as there main graphics engine, but OpenGL is in allot of games as an optional graphics choice. (I think).
02-04-2011, 11:40 PM #9
Ritztro
I am a Game Developer
Originally posted by TheUberFail View Post
Most modern games use DirectX I think as there main graphics engine, but OpenGL is in allot of games as an optional graphics choice. (I think).


OpenGl is what every game for ps3 is. Directx is good for windows and that is it. OpenGL is powerfull and can be on pretty much anything.
02-04-2011, 11:58 PM #10
Originally posted by Dutch View Post
OpenGl is what every game for ps3 is. Directx is good for windows and that is it. OpenGL is powerfull and can be on pretty much anything.



Cool, thanks for that.

Btw, how do i access this map

map <int, string> index;

index[int]==myString

would this syntax work?, i get errors trying to do this:

index[id]=username;
account[index[id]]=password;

its so i can dynamically add accounts to a class. ( and create a usefully log in system )

---------- Post added at 06:58 PM ---------- Previous post was at 06:58 PM ----------

Full source code (Need drawMenu.h to compile) You must login or register to view this content.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo