reyzaku1

Code 2

Jul 10th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. string user;
  8. string pass;
  9.  
  10. int LoginCheck(string user, string pass)
  11. {
  12. ifstream file;
  13. string username, password;
  14. int n = 0;
  15. file.open("users.txt");
  16. if (file.is_open())
  17. {
  18. while (!file.eof())
  19. {
  20. file >> username >> password;
  21. n++;
  22. if (user == username && pass == password)
  23. return n;
  24. }
  25. }
  26. else
  27. {
  28. cout << "file not open" << endl;
  29. }
  30. return 0;
  31. }
  32.  
  33. void Register()
  34. {
  35. ifstream file;
  36. ofstream newuser;
  37. string username, password, passwordconfirm;
  38. file.open("users.txt", ios::app);
  39. newuser.open("users.txt", ios::app);
  40. bool uservalid = false;
  41. while (!uservalid)
  42. {
  43. cout << "Username: ";
  44. cin >> username;
  45. cout << "Password: ";
  46. cin >> password;
  47. cout << "Confirm password: ";
  48. cin >> passwordconfirm;
  49. int m = 0;
  50. int k = 0;
  51. while (file >> user >> pass)
  52. {
  53. m++;
  54. if (username != user)
  55. k++;
  56. }
  57. if (m == k && password == passwordconfirm)
  58. uservalid = true;
  59. else if (m != k)
  60. cout << "There is already a user with this username." << endl;
  61. else
  62. cout << "The passwords given do not match." << endl;
  63. }
  64. newuser << username << " " << password << endl;;
  65. file.close();
  66. newuser.close();
  67. }
  68.  
  69. int main()
  70. {
  71. int loginattempts = 0;
  72. ifstream userfile;
  73. userfile.open("users.txt");
  74. string userset, passset;
  75. if (!userfile.is_open())
  76. {
  77. cout << "file not found" << endl;
  78. }
  79. else
  80. {
  81. cout << "1.Login \n2.Register" << endl;
  82. int option;
  83. cin >> option;
  84. if (option == 1)
  85. {
  86. while (LoginCheck(user, pass) == 0)
  87. {
  88. loginattempts++;
  89. cout << "Username: ";
  90. cin >> user;
  91. cout << "Password: ";
  92. cin >> pass;
  93. if (LoginCheck(user, pass) != 0)
  94. cout << "Welcome " << user << "." << endl;
  95. else if (loginattempts == 3)
  96. {
  97. cout << "Maximum login attempts exceeded." << endl;
  98. break;
  99. }
  100. else
  101. {
  102. cout << "Invalid username/password combination" << endl;
  103. }
  104. }
  105. userfile.close();
  106. }
  107. else if (option == 2)
  108. {
  109. Register();
  110. }
  111. }
  112. int a;
  113. cin >> a; // this has no purpose other than stopping the program closing automatically
  114. return 0;
  115. }
Add Comment
Please, Sign In to add comment