我的反击不起作用?

My pushback isn't working?

本文关键字:不起作用 反击 我的      更新时间:2023-10-16

my board cpp

#include "BBoard.h"
#include <fstream>
#include <algorithm>
using namespace std;
User user_l;
BBoard::BBoard(){
    title = "Default BBoard";
    vector<User> user_list;
    User current_user;
    vector<Message> message_list;
}
BBoard::BBoard(const string &ttl){
    title = ttl;
}
void BBoard::setup(const string &input_file){
    ifstream fin;;
    fin.open(input_file.c_str());
    while(!fin.eof()){
        user_list.push_back(user_l);
    }
}
bool BBoard::user_exists(const string &name, const string &pass) const{
    User copy(name, pass);
    vector<User>::const_iterator i = 
        find(user_list.begin(), user_list.end(), copy);
    return !(i == user_list.end());
}
void BBoard::login(){
    string sn, pw;
    cout << "Welcome to " << title;
    bookmark:
    cout << "nEnter your username ('Q' or 'q' to quit): ";
    getline(cin, sn);
    if((sn == "Q" || sn == "q")){
        cout << "Bye!";
        exit(0);
    }
    cout << "Enter your password: ";
    getline(cin, pw);
    user_exists(sn, pw);
    if(user_exists(sn, pw) == false){
        cout << "Invalid Username or Password!" << endl;
        goto bookmark;
    }
    else{
        cout << "Welcome back " << sn << "!";
    }
}

和标题

#ifndef BBOARD_H
#define BBOARD_H
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class User
{
public:
    User() { }
    User(const std::string& _name, const std::string& _pass)
        : name(_name), pass(_pass)
    { }
    friend bool operator==(const User& lhs, const User& rhs)
    {
        return (lhs.name == rhs.name) &&
               (lhs.pass == rhs.pass);
    }
private:
    std::string name;
    std::string pass;
};
class Message{
};
class BBoard{
private:
    string title;
    vector<User> user_list;
    User current_user;
    vector<Message> message_list;
public:
    BBoard();
    BBoard(const string &ttl);
    void setup(const string &input_file);
    void login();
    void run();
private:
    //void add_user(istream &infile, const string &name, const string &pass);
    bool user_exists(const string &name, const string &pass) const;
    //User get_user(const string &name) const;
    //void display() const;
    //void add_message();
};
#endif

与主

#include "BBoard.h"
#include <string>
#include <cstring>
using namespace std;
int main(int argc, char* argv[]){
    BBoard board;
    string name;
    cout << "Test" << endl;
    name = argv[1];
    cout << "Test" << endl;
    cout << name << endl;
    board.setup(name);
    cout << "Test" << endl;
    board.login();
    cout << "Test" << endl;
}
对于我的程序,

我设置了打印以查看我的程序无法正常工作的地方,并将其定位在board.setup(name)。 我认为我通过从文本文件中读取内容来反击是无效的,因为它只是在 Visual Studio 中不断为我闪烁下划线。 我是否错误地设置了向量和回推?

setup函数中的问题是您实际上并没有从文件中读取任何内容。您只需在无限循环中推送一个空字符串。


哦,永远不要做while (!fin.eof()),它不会像你期望的那样工作。原因是在您尝试从文件末尾读取之前,不会设置 eofbit 标志

我建议你这样做,例如

while (std::getline(fin, user_l))
    user_list.push_back(user_l);

或者可能

while (fin >> user_l)
    user_list.push_back(user_l);

(取决于文件格式。