Alright, so I need a program that will read to see if a database file exists, and if it doesn't, asks the user to create a username and password, then put them in the database file. Then regardless of the result of the file check, ask to login. When the user logs in, I want it to be the username and password that is in the database file.
ALSO:
I want to be able to have multiple users.
I would PREFER to have one database file, and not two, as this complicates things.
Say there are 5 users. I want user 4 to be able to edit JUST their username and password from the database.
I want to have a guest mode.
I want to have ONE admin.
I know that is a lot, but I have a code that will do most of it, there are just a few problems.
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
|
#include <cstdlib>
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include "functions.h"
using namespace std;
const int SIZE = 100; // Always use MORE than required
string Usernames[SIZE]; // From the database
string Password[SIZE]; // From the database
int GetNameIndex(string query, int size)
{
for (int i=0; i<size; i++) //Prevents the program from reading garbage
{
if (query == Usernames[i]) return i; //Return the index
}
return -1; //Error code
}
bool PasswordMatches(int index, string passwd)
{
return (passwd == Password[index]);
}
int main(int argc, char** argv) {
initUserCheck(); // Checks to make sure that a user is registered, if not creates a new one.
//Starts the Login Process.
//Read the database:
const char *appdata = std::getenv ( "APPDATA" );
std::string path = appdata;
path += "\\myprogram\\files\\database.dtbf";
std::ifstream fin ( path.c_str() );
int i=0;
while (!fin.eof()){
fin >> Usernames[i] >> Password[i];
i++; //This represents the number of lines we could extract from the database
}
//Begin VISUAL login
RetryLogin:
string usrname, passwd;
printf("Username: ");
cin >> usrname;
int index = GetNameIndex(usrname, i); //Send the database size to this function.
printf("Password: ");
cin >> passwd;
if (!PasswordMatches(index, passwd)){ // If Password+Username doesn't match.
printf("Correct Username, incorrect Password. Please try again. \n");
system("PAUSE");
goto RetryLogin;
}
fin.close();
printf("You have successfully logged in.\n\n");
// Rest of the Program...
|
Probems with this code:
If a user enters a wrong username, the program freezes, and then becomes unresponsive (in English, it crashes). There is no guest, or admin mode.
Last thing:
lets say that the file, filename has this:
sillyguy
password
mike
legos
sarah
ilikepie
|
How does mike, edit JUST their username and password, without messing with everyone elses?
Thanks!!
If you don't get anything about this, I will get back to you ASAP.