C++ 代码编译没有错误,但没有给出输出

问题描述 投票:0回答:2

好的,所以我正在尝试编写一个程序,将密码和用户名存储在一个文件中(我还没有写他们写入文件的部分,稍后会写),但是我的代码部分读取文件导致它无法工作(无输出),即使它在 VS 代码中编译没有错误。老实说,我也不完全理解我写的东西(我只是看了一个 youtube 视频),所以我一定是遗漏了一些关于代码的东西。 有关额外信息,我使用 C++ 20 和 clang++ 作为我的编译器。 我已经在下面发布了我的代码以供参考。

#include <iostream>
#include <cstdio>
#include <vector>
#include <fstream>


using namespace::std;

class user {
    private:
        string username;
        string password;
    public:
        user(string user, string pass) {
            username = user;
            password = pass;
        }
        void SetUsername(string user) {
            username = user;
        }
        void SetPassword(string pass) {
            password = pass;
        }
        void ChangeUsername(string user) {
            username = user;
        }
        void ChangePassword(string pass) {
            password = pass;
        }
        string GetUsername() {
            return username;
        }
        string GetPassword() {
            return password;
        }
        
        

};

int main() {
    string listUsernames[1000];
    string listPasswords[1000];
    vector <user>list;
    string username;
    string password;
    string prompt;
    bool loginSuccess = false;
    bool success = false;
    int counter = 0;
    int lines = 0;

    /*
    This part is like messsing it up
    Starting from here:
    */
    ifstream file;
    file.open("usernames.txt");
    while(!file.eof()) {
        getline(file, listUsernames[lines]);
        lines++;
    }
    file.close();


    lines = 0;
    file.open("passwords.txt");
    while(!file.eof()) {
        getline(file, listPasswords[lines]);
        lines++;
    }
    file.close();

    for(int i = 0; i < lines; i++) {
        user oldUser(listUsernames[i], listPasswords[i]);
        list.push_back(oldUser);
    }
    /*
    Ending here
    */

    cout << "\tWelcome user! Please choose to login or create an account below.\n";
    do{
    cout << "\tType \"Login\" to login or \"Create\" to create a new account\n";
    cin >> prompt;
        if(prompt == "Login") {
            do{
                cout << "Username: ";
                cin >> username;
                cout << "Password: ";
                cin >> password;

                for(int i = 0; i < list.size(); i++) {
                    if(username == list[i].GetUsername() && password == list[i].GetPassword()) {
                        cout << "Hello " << list[i].GetUsername() << "!\n\n";
                        loginSuccess = true;
                        success = true;
                    }
                }
                if(!loginSuccess) {
                    cout << "Incorrect username or password";
                    counter++;
                    if(counter < 2) {
                        cout << ", please try again.\n";
                    }
                    else {
                        cout << "\nMaybe you should try creating an account! \n";
                    }
                }
            } while(!loginSuccess && counter < 2);
        }
        else if(prompt == "Create") {
            string newUsername;
            string newPassword;
            cout << "What would you like your username to be? Please enter: ";
            cin >> newUsername;
            cout << "What would you like your password to be? Please enter: ";
            cin >> newPassword;  
            user newUser(newUsername, newPassword);
            list.push_back(newUser);
            cout << "Success!\n";
            ofstream myfile;
            myfile.open("usernames");
            myfile << newUsername;
            myfile.close();
            myfile.open("passwords");
            myfile << newPassword;
            myfile.close();
        }
        else {
            cout << "Sorry, that input was not understood\n";
            cout << "Please try again\n";
        }
    } while(!success);
    return 0;
}
c++ output
2个回答
0
投票

你的错误:

  • 您正在写入文件“username”和“passwords”并从“usernames.txt”和“passwords.txt”中读取。
  • 你正在检查
    while (!file.eof())
    这是不正确的(链接

修复后:

ifstream file;
file.open("usernames");
while(getline(file, listUsernames[lines])) {
    lines++;
}
file.close();


lines = 0;
file.open("passwords");
while(getline(file, listPasswords[lines])) {
    lines++;
}
file.close();

0
投票
#include <iostream>
#include <cstdio>
#include <vector>
#include <fstream>
#include <cassert>

struct User
{
    std::string username;
    std::string password;
};

std::vector<std::string> from_file(const std::string& file_name)
{
    auto content = std::vector<std::string>{};
    auto line = std::string{};
    auto file = std::ifstream{file_name};
    while(getline(file, line)) {
        content.emplace_back(std::move(line));
    }
    return content;
}

bool login( const std::vector<User>& users )
{
    constexpr auto max_retry = 2;
    auto counter = 0U;
    while(counter < max_retry)
    {
        auto username = std::string{};
        auto password = std::string{};
        std::cout << "Username: ";
        std::cin >> username;
        std::cout << "Password: ";
        std::cin >> password;

        for(const auto& user : users) {
            if(user.username == username && user.password == password) {
                std::cout << "Hello " << username << "!\n\n";
                return true;
            }
        }

        std::cout << "Incorrect username or password";
        ++counter;
        if(counter < max_retry) {
            std::cout << ", please try again.\n";
        }
        else {
            std::cout << "\nMaybe you should try creating an account! \n";
        }
    }
    return false;
}

constexpr auto usernames_file = "usernames.txt";
constexpr auto passwords_file = "passwords.txt";

bool create(std::vector<User>& users)
{
    auto username = std::string{};
    auto password = std::string{};
    std::cout << "What would you like your username to be? Please enter: ";
    std::cin >> username;
    std::cout << "What would you like your password to be? Please enter: ";
    std::cin >> password;
    users.emplace_back(User{username, password});
    std::cout << "Success!\n";
    {
        auto file = std::ofstream{usernames_file};
        file << username;
    }
    {
        auto file = std::ofstream{passwords_file};
        file << password;
    }
    return true;
}

int main()
{
    auto list_usernames = from_file(usernames_file);
    auto list_passwords = from_file(passwords_file);

    assert(list_usernames.size() == list_passwords.size());

    auto users = std::vector<User>{};
    for(auto i = 0; i < list_usernames.size(); ++i)
    {
        users.emplace_back(User{list_usernames[i], list_passwords[i]});
    }

    std::cout << "\tWelcome user! Please choose to login or create an account below.\n";
    auto success = false;
    while(!success)
    {
        std::string prompt;
        std::cout << "\tType \"Login\" to login or \"Create\" to create a new account\n";
        std::cin >> prompt;
        if(prompt == "Login")
        {
            success = login(users);
        }
        else if(prompt == "Create")
        {
            success = create(users);
        }
        else
        {
            std::cout << "Sorry, that input was not understood\n";
            std::cout << "Please try again\n";
        }
    }
    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.