Login

excuse me ! help me please! how can i make the program say that if i created an account
ex.
name:aspic
pass:plusplus
and create another with the same name and pass!
how can i make the program say that that account/user is already created!


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
#include<iostream>
#include<fstream>
#include<string.h>
#include<cstring>

using namespace std;

void main();

void login(){
	int game;
	char user_name[20];
	char password[20];
	char e_add[30];
	char user[20];
	char pass;
	char pass2[20];
	char user2[20];
	int lib;
	ofstream account;
	ifstream open_account;
	do{
	cout<<"[1]Login"<<endl;
	cout<<"[2]Create account"<<endl;
	cout<<"[3]Exit"<<endl;
	cin>>game;


	switch(game){
		case 1:

		cout<<"Username:";
		cin>>user;
		open_account.open(user);
		if(open_account){
			while(!open_account.eof()){
				open_account.get(pass);

			}
			open_account.close();
		}


		open_account.close();
		cout<<"Password:";
		cin>>pass2;
		open_account.open(user);
		if(open_account){
			while(!open_account.eof()){
				open_account.getline(user2, 20);
				lib = strcmp(pass2, user2);
				if(lib == 0){
					alternate();
				}
			}
		}
		else{
			cout<<"Incorrect Password or Username!"<<endl;
		}
		break;

		case 2:

			cout<<"Enter User Name:";
			cin.getline(user_name, 20);
			cin.getline(user_name, 20);
			cout<<"Enter Password:";
			cin.getline(password, 20);
			cout<<"Enter Email Add:";
			cin.getline(e_add,30);

			account.open(user_name);
			account<<endl<<user_name<<endl;
			account<<password<<endl;
			account<<e_add<<endl;

			account.close();
		break;
        case 3:
            main();
        break;
        }
	} while(game!=3);


}

void main(){
login();
}
When creating account, you should check entered name if it already in existing account list.
If the number of names you would be storing won't be massively long say over 1000, then you can use a simple linked list data structure or array to hold the names. Otherwise consider using a self balancing tree data structure or last but not least hashtable.

Simple example using stl container std::set

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <set>
#include <string>
#include <utility>
#include <iostream>

class linkedlist : public std::set<std::string> {};

typedef std::pair<std::set<std::string>::iterator, bool> psb;

int main() {
	psb temp;
	linkedlist mylist;
	temp = mylist.insert("Mark");
	if ( !temp.second )
		std::cout << "That username already exists!\nTry again...\n";
	temp = mylist.insert("Mark");
	if ( !temp.second )
		std::cout << "That username already exists!\nTry again...\n";
	return 0;
}
@aspic

So what is with lines 8 and 88? Don't ever do that. Notice Smac89's main function?
class linkedlist : public std::set<std::string> {};
A bad idea.

1) It is not a linked list. Do not confuse those who will read your code.
2) Public inheritance only to create an alias? Really? And you use typedef on the next line...
Use: using nameContainer = std::set<std::string>; or typedef if you still do not use C++11
Another idea is to use C++11's unordered_map. It allows for a key (being the name) as well as the value (or password) to be stored. It does not allow multiple of the same key, but it DOES allow multiple keys having the same value, so people don't get confused when they can't use an account due to the password being the same.\

For example:

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
#include <unordered_map>
#include <string>
#include <iostream>

std::unordered_map<std::string, std::string> accounts;

bool addAccount(std::string name, std::string password) {
    std::pair<std::unordered_map<std::string,std::string>::iterator, bool>
        result = accounts.insert({name, password});

    if (!result.second) {
        std::cout << "Error: Account " << name << " already exists." << std::endl;
        return false;
    }

    return true;
}

int main() {
    addAccount("Bob", "12345");
    addAccount("John", "QWERTYUIOP");
    addAccount("Bob", "password");

    return 0;
}
Last edited on
Topic archived. No new replies allowed.