Alright, so i'm trying to make a type of database using file i/o where you type in a username and password and it writes it to database.txt... The issue is, i'm trying to find a correct and clean way of reading, or 'searching' for the username and password and compares it with what the user put. Here is what I have so far.

(I know it is sloppy, but i typed it up in 5 minutes so ya know. What it does is uses a menu and switch/case for options 1-4, option 1 is where i need help at, to find out how to search for the username correctly. i'm pretty good with strings, its just i really have no clue how i'd do this properly.)

#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

char* Username = new char[255];
char* Password = new char[255];
char* User = new char[255];

void Join() 
{
    ofstream DataBase("DataBase.txt", ios::app);
    cout << "Welcome New Member, Please Enter a New Username and Password:\n\n";
    cout << "Username: ";
    cin >> Username;
    cout << "\nPassword: ";
    cin >> Password;
    
    DataBase << Username << "\n";
    DataBase << Password << "\n\n";
    cout << "\nSaved To Database!\n";
    DataBase.close();
    
    delete [] Username;
    delete [] Password;
}

void Login() {
     
     ifstream DataBase("DataBase.txt");
     cout << "Welcome Member, Please Enter Your Username and Password:\n\n";
     cout << "Username: ";
     cin >> User;
     DataBase >> Username;
     if (Username==User) cout << "\nUsername Correct!";
     else cout << "\nUsername Incorrect!";
              
                        
     
     delete [] User;
     delete [] Username;
     delete [] Password;
     }
     
     void About() {
          }
      

int main(int argc, char *argv[])
{
    
    int option;
    
  cout << "DataBase v1.00 BETA!\n\n";
  cout << "1.Login\n";
  cout << "2.Sign-Up\n";
  cout << "3.About\n";
  cout << "4.Exit\n\n";
  cout << "Option(1-4): ";
  cin >> option;
  
  switch (option) {
         case 1:
              Login();
              break;
         case 2:
              Join();
              break;
         case 3:
              About();
              break;
         case 4:
              break;
         default:
                 cout << "Not An Option!";
                 break;
                 }
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Thanks in advance for any advice, or if anyone can point me in the right direction, i'm much appreciative.

Dani AI

Generated

Several concrete problems prevent the posted program from reliably finding usernames:

  • The code allocates global char* buffers and frees them inside Join() (and again later). That creates dangling pointers and double-deletes. Use local std::string objects instead; do not hand-manage new[]/delete[].
  • Comparing char* with == compares addresses, not text. Use std::string (or std::strcmp) so == compares contents.
  • Using the extraction operator >> and blank lines makes the file layout brittle: whitespace in names/passwords, stray blank lines, or CR/LF differences will break the pairing.

A practical, robust approach: store one account per line, using a safe delimiter (for example : or \t) and a password hash. Read with std::getline, parse the line, and compare. Example pattern:

while (std::getline(db, line)) {
  auto p = line.find(':');
  if (p == std::string::npos) continue;
  std::string name = line.substr(0,p);
  std::string stored = line.substr(p+1);
  if (name == enteredName && verifyHash(enteredPass, stored)) { /* success */ }
}

Notes and cautions: always check that the file opened successfully; trim a trailing \r if the file was produced on Windows; avoid storing plaintext passwords—use a proper password-hashing algorithm (bcrypt/Argon2) or a vetted library; for small programs you can load the file into an std::unordered_map for fast lookups, but for concurrent or large-scale use, switch to a real database and transactional updates (write to a temp file then rename).

As pointed out, skipping lines can work but is fragile; splitting usernames/passwords into separate files (as suggested) makes atomic updates and integrity worse. Use structured single-line records and string-based parsing for reliability.

Recommended Answers

All 2 Replies

what i would do is get rid of the second newline in DataBase << Password << "\n\n"; and have DataBase << Password << "\n"; with one newline in the join function so that file goes
username
password
username
password
...

then in login you could use a while loop and just skip every other line of input from the file because thats is the password and you are just checking the username. otherwise you would want to retrieve the username and password. if you just want the username you could do this

ifstream DataBase("DataBase.txt");
int counter = 1;
cout << "Welcome Member, Please Enter Your Username and Password:\n\n";
cout << "Username: ";
cin >> User;
while (!DataBase.eof())
{
if ((counter % 2) == 0) // this will get the next line in the file then skip
{
   DataBase >> Username;
   continue;
}
DataBase >> Username;
if (Username == User)
//...

Try this code:

#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

char* Username = new char[255];
char* Password = new char[255];
char* User = new char[255];
char* Pass = new char[255];

void Join() 
{
    ofstream username("Username.txt");
    ofstream password("Password.txt");
    cout << "Welcome New Member, Please Enter a New Username and Password:\n\n";
    cout << "Username: ";
    cin >> Username;
    cout << "\nPassword: ";
    cin >> Password;
    
    username << Username << "\n";
    password << Password << "\n\n";
    cout << "\nSaved To Database!\n";
    username.close();
    password.close();
    
    delete [] Username;
    delete [] Password;
}

void Login() {
     
     ifstream username("Username.txt");
     cout << "Welcome Member, Please Enter Your Username and Password:\n\n";
     cout << "Username: ";
     cin >> User;
     username >> Username;
     string user = Username;
     

     if (user==User) cout << "\nUsername Correct!\n";
     else cout << "\nUsername Incorrect!\n";

     ifstream password("Password.txt");
     cout << "Password: ";
     cin >> Pass;
     password >> Password;
     string pass = Password;
     if (pass==Password) cout << "\nPassword Correct!\n";
     else cout << "\nPassword Incorrect!\n";

     
              
                        
     
     delete [] User;
     delete [] Username;
     delete [] Password;
     }
     
     void About() {
          }
      

int main(int argc, char *argv[])
{
    
    int option;
    
  cout << "DataBase v1.00 BETA!\n\n";
  cout << "1.Login\n";
  cout << "2.Sign-Up\n";
  cout << "3.About\n";
  cout << "4.Exit\n\n";
  cout << "Option(1-4): ";
  cin >> option;
  
  switch (option) {
         case 1:
              Login();
              break;
         case 2:
              Join();
              break;
         case 3:
              About();
              break;
         case 4:
              break;
         default:
                 cout << "Not An Option!";
                 break;
                 }
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

It has different text file for username and password. Hope this will help! ^^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.