Advertisement
dredLuxx

Untitled

Dec 28th, 2019
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. //#include<cstring>
  4. //for C strings
  5. using namespace std;
  6. int main()
  7. {
  8. string
  9. command, //to choose what to do each time
  10. name, password, //the ones that are found in the file
  11. inName, inPassword, //the ones you are going to input from keyboard
  12. registerName, registerPassword; //also what you're going to input
  13. //and if you know C strings, just replace that with something like
  14. /*
  15. char
  16. command[9],
  17. name[31], password[31], //it could be any size, but like this you have got 30 characters at your
  18. //disposal, if you consider it to be enough
  19. inName[31], inPassword[31],
  20. registerName[31], registerPassword[31];
  21. */
  22. while (1)
  23. {
  24. cout<<"(register/exit/login)\n"
  25. <<"Command: ";
  26. getline(cin, command);
  27. //(for C strings)
  28. //cin.get(command, 9);
  29. //cin.get();
  30. if (command=="exit") //(for C strings) if (!strcmp(command, "exit"))
  31. {
  32. return 1;
  33. }
  34. if (command=="register") //(for C strings) if (!strcmp(command, "register"))
  35. {
  36. //open file for registration
  37. ofstream g("registration.txt"); //ofstream is the one for getting data from the file,
  38. //and the file does not even have to exist. If it's ofstream, it'll take care of it for you.
  39. //but be warned that if there is a file called "registration.txt" in the name folder as the
  40. //.exe file, the contents will be deleted
  41. if (!g.is_open()) //if it's not open, then there is no such file with the given name inside
  42. //the folder (that is, in the folder where the .exe file is going to be)
  43. {
  44. cout<<"could not open file\n"; //just so that you know why it won't work if it doesn't
  45. return 0;
  46. }
  47. cout<<"\n\n\n" //3 newlines
  48. <<"New Username: ";
  49. getline(cin, registerName); //input from keyboard will go into registerName
  50. cout<<"New Password: ";
  51. getline(cin, registerPassword); //input from keyboard will go into registerPassword
  52. g<<registerName; //this basically says "put whatever's to the right (registerName) into
  53. //g ("registration.txt")".
  54. g<<'\n'; //and now there will be a new line
  55. g<<registerPassword; //and now the password
  56. //all placed safely in the file that g opened
  57. g.close(); //always make sure you close the file, or else you might end up with some nasty
  58. //stuff in the memory
  59. }
  60. if (command=="login") //(for C strings) if (!strcmp(command, "login"))
  61. {
  62. //open file, and then put the name and password into the strings
  63. ifstream f("registration.txt"); //ifstream is the one for getting data from the file, and
  64. //let us assume you've already created a file called "registration.txt"
  65. if (!f.is_open()) //if it's not open, then there is no such file with the given name inside
  66. //the folder (that is, in the folder where the .exe file is going to be)
  67. {
  68. cout<<"could not open file\n"; //just so that you know why it won't work if it doesn't
  69. return 0;
  70. }
  71. getline(f, name, '\n'); //reads the user name from file f (which is using "registration.txt")
  72. getline(f, password, '\n'); //reads the password from file f (which is using "registration.txt")
  73. //also, if you tell the file to get you that text up until '\n', that's when you know it reads
  74. //the whole line at most, and won't go any further
  75. //and that is done by the 3rd parameter
  76. f.close(); //you don't need it open now, since you have the name and password from the file
  77.  
  78. //login
  79. while (1)
  80. {
  81. //you are going to input the name and password here
  82. cout<<"\n\n\n"
  83. <<"Enter Username: ";
  84. getline(cin, inName);
  85. cout<<"Enter Password: ";
  86. getline(cin, inPassword);
  87. //or this, if you are working with C strings (second version of declaration)
  88. //cin.get(inName, 31);
  89. //cin.get();
  90. //cin.get(inPassword, 31);
  91. //cin.get();
  92. //and the "cin.get()" after each input line is necessary, or else[...]
  93. //no idea what's happening inside istream, but it's mandatory if you don't want your
  94. //input to get stuck or worse
  95. if (inName==name && inPassword==password)
  96. {
  97. cout<<"Login Successful\n" //the '\n' is a character, so that's why I can add it
  98. //and it will automatically output a newline in console, alongside the string
  99. <<"Welcome, "
  100. <<inName;
  101. break; //just exit the while loop if you've entered the valid account
  102. }
  103. cout<<"incorrect name or password\n"; //if you haven't entered the valid account,
  104. //then the while loop is not done yet. So that's why this output is without condition
  105. }
  106. //now do something about the account
  107. }
  108. cout<<"\n\n\n\n\n"; //give it 5 newlines
  109. }
  110. return 1;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement