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-05-2011, 12:07 AM #11
Ritztro
I am a Game Developer
Originally posted by TheUberFail View Post
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.


So what exactly are you trying to do? Why do you have an index? Why dont you just have username and pass like I have above? What exactly is the index for?
02-05-2011, 12:12 AM #12
Originally posted by Dutch View Post
So what exactly are you trying to do? Why do you have an index? Why dont you just have username and pass like I have above? What exactly is the index for?


The index is for keeping track of how many accounts have been made, so i can dynamically add and delete accounts. its just each username and passwords unique ID.
02-05-2011, 01:48 AM #13
Ritztro
I am a Game Developer
Originally posted by TheUberFail View Post
The index is for keeping track of how many accounts have been made, so i can dynamically add and delete accounts. its just each username and passwords unique ID.


Oh, well you could just have an int that adds each time.. Because why do you need a specific id when you could just delete by username??
02-05-2011, 02:00 AM #14
Originally posted by Dutch View Post
Oh, well you could just have an int that adds each time.. Because why do you need a specific id when you could just delete by username??


The id is also to keep track of how many accounts there are for displaying, especially in the menus.


here compile this, its finished, and it works really nice
    #include <iostream>
#include <windows.h>
#include <map>
#include "drawMenu.h"

using namespace std;

class CLogin
{
public:
map<string,string> account;
map<int,string> index;
bool add(string username,string password,int id);
}instance;

bool CLogin::add(string username,string password,int id)
{
for(int dynamicId=1 ; dynamicId<=id ; dynamicId++)
{
if(index[dynamicId]==username)
{
return false;
}
}

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

return true;
}

void createAccount(int& numberOfAccounts)
{
string tempName, tempPass;
for(;Winky Winky
{
system("CLS");
cout<<"\n\tEnter username: ";getline(cin,tempName);
cout<<"\tEnter password: ";getline(cin,tempPass);

if(instance.add(tempName,tempPass,numberOfAccounts))
{
numberOfAccounts++;
return;
}

system("CLS");
cout<<"\n\tAccount already exists!";
Sleep(2000);
}
}

void displayAccounts(int numberOfAccounts)
{
system("CLS");
cout<<endl;

for(int i=1 ; i<=numberOfAccounts ; i++)
{
cout<<"\tAccout "<<i<<": "<<instance.index[i]<<endl;
}
cout<<"\t";
system("PAUSE");
}

void removeAccount(int toRemove, int& accounts)
{
for( ; toRemove < accounts ; toRemove++)
{
instance.index[toRemove]=instance.index[toRemove+1];
instance.account[instance.index[toRemove]]=instance.account[instance.index[toRemove+1]];
}
instance.index.erase(accounts);
instance.account.erase(instance.index[accounts]);
}

void deleteAccount(int& id)
{
string names;
if(id == 0)
{
cout<<"\n\tNo accounts to delete!";
Sleep(2500);
return;
}
names = "";
names += instance.index[1];
for(int i=2 ; i < id ; i++)
{
names += "|";
names += instance.index[i];
}
if(id>=2)
{
names += "|";
names+=instance.index[id];
}

cout<<endl;

removeAccount(drawMenu("Select an account to delete",names,1),id);
id--;
return;
}

int main(void)
{
int numberOfAccounts = 0;

for(;Winky Winky
{
system("CLS");
cout<<endl;
switch(drawMenu("Loging Server By TheUberFail","Create Account|Display Accounts|Delete account|Exit",1))
{
case 1: createAccount(numberOfAccounts);break;
case 2: displayAccounts(numberOfAccounts);break;
case 3: deleteAccount(numberOfAccounts);break;
case 4: return EXIT_SUCCESS;
}
}
}


remember to include drawMenu.h

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo