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.

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.