我的董事会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;
}
对于我在main中的程序,我设置了打印件,以查看我的程序无法正常工作的位置,并将其放在board.setup(name)中。我认为通过从文本文件中读取内容的推迟是不起作用的,因为它只是在Visual Studio中为我闪烁下划线。我是否设置了向量并推回错误了?
答案 0 :(得分:5)
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);
(取决于您的文件格式。)