Advertisement
Guest User

Untitled

a guest
Aug 9th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <iomanip>
  5. using namespace std;
  6. void rgs();
  7. void Menu();
  8. void Login();
  9.  
  10. int ans1;
  11. string charname, pw, user, pass;
  12.  
  13.  
  14. int main()
  15. {
  16. ifstream inFile;
  17. inFile.open("users.txt");
  18. string userset, passset;
  19. if (!inFile.is_open())
  20. {
  21. cout << "There is an error." << endl;
  22. }
  23. else
  24. {
  25. Menu();
  26. system("CLS");
  27. if (ans1==1)
  28. {
  29. Login();
  30. }
  31. else if (ans1==2)
  32. {
  33. rgs();
  34. }
  35. else
  36. {
  37. cout<<"You have entered an invalid option"<<endl;
  38. }
  39. system ("pause");
  40. return 0;
  41. }
  42. }
  43. void Login()
  44. {
  45. ifstream inFile;
  46. inFile.open("users.txt");
  47. if (!inFile)
  48. {
  49. cout<<"Unable to load the database"<<endl;
  50. }
  51. else
  52. {
  53. cout << "Please enter your username: ";
  54. cin>> user;
  55. cout << "Please enter your password: ";
  56. cin >> pass;
  57.  
  58. inFile>>charname>>pw;
  59. if (inFile.fail())
  60. {
  61. cout<<"Error"<<endl;
  62. }
  63. else if (charname==user && pw==pass)
  64. {
  65. cout<<"You have log on successfully."<<endl;
  66. }
  67. else
  68. {
  69. cout<<"You have entered the wrong username/password"<<endl;
  70. }
  71. }
  72.  
  73. }
  74.  
  75.  
  76. void Menu()
  77. {
  78. cout<<"Welcome to dungeon hunter!"<<endl;
  79. cout<<"Choose a choice below."<<endl;
  80. cout<<"1. Login"<<endl;
  81. cout<<"2. Register a new account"<<endl;
  82. cout<<"Please enter your option: ";
  83. cin>>ans1;
  84. }
  85.  
  86. void rgs()
  87. {
  88. ifstream inFile;
  89. ofstream newuser;
  90. string username, password, passwordconfirm;
  91. inFile.open("users.txt", ios::app);
  92. newuser.open("users.txt", ios::app);
  93. bool uservalid=false;
  94. while (!uservalid)
  95. {
  96. cout << "Username: ";
  97. cin >> username;
  98. cout << "Password: ";
  99. cin >> password;
  100. cout << "Confirm password: ";
  101. cin >> passwordconfirm;
  102. int m=0;
  103. int k=0;
  104. while (inFile >> user >> pass)
  105. {
  106. m++;
  107. if (username!=user)
  108. k++;
  109. }
  110. if (m==k && password==passwordconfirm)
  111. uservalid=true;
  112. else if (m!=k)
  113. cout << "There is already a user with this username." << endl;
  114. else
  115. cout << "The passwords given do not match." << endl;
  116. }
  117. newuser << username << " " << password << endl;;
  118. inFile.close();
  119. newuser.close();
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement