Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,227 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,014 people online right now. Registration is fast and FREE... Join Now!





Check username and password from file

this program reads one username and one password from a file. and then verifies them to be correct. also features a utility to change username and password, but requires the current password. Compiled with Dev-C++

Submitted By: cipherence
Actions:
Rating:
Views: 8,012

Language: C++

Last Modified: August 23, 2006
Instructions: create a file called username.txt and a file called password.txt . obviously put one username in the user file and one pass in the pass file. no blanks are accepted.

Snippet


  1. // written by Cipherence
  2. // compiled with Dev-C++
  3. // use, re-write, claim as your own. whatever
  4. // hope you learn, it lacks comments
  5. #include<iostream>
  6. #include<fstream>
  7. #include<conio.h>
  8.  
  9. using namespace std;
  10.  
  11. void Auth();
  12. void Members();
  13. void Userchange();
  14. void Passchange();
  15.  
  16. string inuser;
  17. string inpass;
  18. string user;
  19. string pass;
  20. int num = 0;
  21. string com;
  22.  
  23. main()
  24. {
  25.       system("cls");
  26.       cout<<"Welcome\n---------------------------\n\n";
  27.       while(num==0)
  28.       {
  29.       system("cls");
  30.       cout<<"Command: ";
  31.       cin>>com;
  32.       if(com=="login")
  33.       {
  34.       Auth();
  35.       }
  36.       else if(com=="change-pass")
  37.       {
  38.            Passchange();
  39.            }
  40.       else if(com=="change-user")
  41.       {
  42.            Userchange();
  43.            }
  44.       else if(com=="credits")
  45.       {
  46.            cout<<"written by Jason Parker"; // cuz it was
  47.            }
  48.       else if(com=="exit")
  49.       {
  50.            cout<<"written by Cipherence SoftWurx";
  51.            getch();
  52.            break;
  53.            }
  54.       else if(com==com)
  55.       {
  56.            cout<<"Unknown command\n";
  57.            }
  58.       }
  59. }
  60. void Auth()
  61. {
  62.      ifstream Passfile("password.txt", ios::in);
  63.      Passfile>>inpass;
  64.      ifstream Userfile("username.txt", ios::in);
  65.      Userfile>>inuser;
  66.      system("cls");
  67.      cout<<"USERNAME: ";
  68.      cin>>user;
  69.      cout<<"PASSWORD: ";
  70.      cin>>pass;
  71.      Userfile.close();
  72.      Passfile.close();
  73.      if(user==inuser&&pass==inpass)
  74.      {
  75.      cout<<"\nHit enter to continue to members area";
  76.      getch();
  77.      Members();
  78.      }
  79.      else
  80.      {
  81.          cout<<"nope";
  82.          getch();
  83.          main();
  84.          }
  85. }
  86. void Userchange()
  87. {
  88.      string tempass;
  89.      system("cls");
  90.      cout<<"enter current password: ";
  91.      cin>>tempass;
  92.      if(tempass==inpass)
  93.      {
  94.      cout<<"utility to change username stored in file\n";
  95.      cout<<"CHANGE USER TO: ";
  96.      cin>>user;
  97.      ofstream Userfile("username.txt", ios::out);
  98.      Userfile<<user;
  99.      Userfile.close();
  100.      cout<<"Username successfully changed";
  101.      getch();
  102.      main();
  103. }
  104. else
  105. {
  106.     cout<<"Invalid Password";
  107.     getch();
  108.     main();
  109. }
  110. }
  111. void Passchange()
  112. {
  113.      string tempass;
  114.      system("cls");
  115.      cout<<"enter current password: ";
  116.      cin>>tempass;
  117.      if(tempass==inpass)
  118.      {
  119.      cout<<"utility to change password stored in file\n";
  120.      cout<<"CHANGE PASS TO: ";
  121.      cin>>user;
  122.      ofstream Passfile("password.txt", ios::out);
  123.      Passfile<<user;
  124.      Passfile.close();
  125.      cout<<"password successfully changed";
  126.      getch();
  127.      main();
  128. }
  129. else
  130. {
  131.     cout<<"Invalid Password: ";
  132.     getch();
  133.     main();
  134. }
  135. }
  136. void Members()
  137. {
  138.      cout<<"nothing";// put members code here
  139.      // like a while(){} loop or somethin like that
  140.      // comment please
  141. }
  142.  

Copy & Paste


Comments


Acetolyne 2007-12-08 21:53:27

Great tutorial shows how to input text from a file which is a spot people often get stuck at this will come in handy for many people i believe thanx for posting this tutorial

Re@p3rM4n 2008-05-07 18:38:09

how would you do this so that there exit multiple acceptable usernames and passwords?

ayya14 2008-08-11 21:32:51

how would you implement this with multiple usernames and passwords in a single file?


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month