Untitled

mail@pastecode.io avatar
unknown
c_cpp
5 months ago
1.0 kB
3
Indexable
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

bool userExists(const string& username, const string& filename)
{
    ifstream file(filename);

    string line;
    while (getline(file, line))
    {
        if (line == username)
        {
            file.close();
            return true;
        }
    }

    file.close();

    return false;
}

void saveUser(const string& username, const string& filename)
{
    ofstream file(filename, ios::app);

    if (file.is_open())
    {
        file << username << endl;
        file.close();
    }
}

int main() 
{
    string filename = "users.txt";
    string username;

    cout << "Podaj swoj psudonim: ";
    cin >> username;

    if (userExists(username, filename))
    {
        cout << "Pseudonim '" << username << "' juz istnieje. Witaj ponownie!" << endl;
    }
    else
    {
        cout << "Witaj w systmie, " << username << "!" << endl;
        saveUser(username, filename);
    }

    return 0;
}
Leave a Comment