我的反击不起作用?
My pushback isn't working?
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);
(取决于文件格式。
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- 在其他文件中创建类时在 c++ 项目中不起作用
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 为什么二进制搜索在我的测试中不起作用
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 带有指定长度字符* 参数的 std::regex_search 在 VS2017 中不起作用?
- Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称)
- 为什么简单的算术减法在"if"条件下不起作用?
- 为什么Stroustup书中的has_f不起作用
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- 嵌套While循环不起作用(C++问题)
- C++Matching Brackets 2解决方案不起作用
- 为什么这段代码不起作用,我该如何解决?
- 我的反击不起作用?