Advertisement
Guest User

Untitled

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