Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- class UserManager
- {
- private:
- const std::string fileName = "users.txt";
- public:
- void create_account()
- {
- std::string username, password;
- std::cout << "Podaj nazwę użytkownika: ";
- std::cin >> username;
- std::cout << "Podaj hasło: ";
- std::cin >> password;
- std::ofstream outFile(fileName, std::ios::app);
- if (outFile.is_open())
- {
- outFile << username << " " << password << std::endl;
- std::cout << "Konto zostało utworzone!" << std::endl;
- outFile.close();
- }
- else
- {
- std::cerr << "Nie można otworzyć pliku." << std::endl;
- }
- }
- bool login()
- {
- std::string username, password;
- std::cout << "Podaj nazwę użytkownika: ";
- std::cin >> username;
- std::cout << "Podaj hasło: ";
- std::cin >> password;
- std::ifstream inFile(fileName);
- if (inFile.is_open())
- {
- std::string fileUsername, filePassword;
- while (inFile >> fileUsername >> filePassword)
- {
- if (fileUsername == username && filePassword == password)
- {
- std::cout << "Zalogowano pomyślnie!" << std::endl;
- inFile.close();
- return true;
- }
- }
- std::cout << "Błędna nazwa użytkownika lub hasło!" << std::endl;
- inFile.close();
- }
- else
- {
- std::cerr << "Nie można otworzyć pliku." << std::endl;
- }
- return false;
- }
- };
- int main()
- {
- UserManager userManager;
- int choice;
- do
- {
- std::cout << "1. Utwórz konto" << std::endl;
- std::cout << "2. Zaloguj się" << std::endl;
- std::cout << "3. Wyjdź" << std::endl;
- std::cout << "Wybierz opcję: ";
- std::cin >> choice;
- switch (choice)
- {
- case 1:
- userManager.create_account();
- break;
- case 2:
- if (userManager.login())
- {
- std::cout << "Koniec programu!" << std::endl;
- choice = 3;
- }
- else
- {
- std::cout << "Spróbuj ponownie!" << std::endl;
- }
- break;
- case 3:
- std::cout << "Do widzenia!" << std::endl;
- break;
- default:
- std::cout << "Nieznana opcja!" << std::endl;
- break;
- }
- } while (choice != 3);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement