1

For a textfile that has usernames and passwords with end being the sentinel

user1 password1 user2 password2 end

How do I add user and password to a vector? I am trying to keep the same user and password combination but only have

ifstream fin;
fin.open("users1.txt");
while(fin != "end"){
    user_list.push_back(user_l);
}

for vector

vector<User> user_list;

edit:

user1 password1

user2 password2

end

edit 2:

#include "BBoard.h"
#include <fstream>
using namespace std;

User user_l;
BBoard::BBoard(){
    title = "Hello World";
    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("users1.txt");
    while(!fin.eof()){
        user_list.push_back(user_l);
    }
}

header

#ifndef BBOARD_H
#define BBOARD_H

#include <iostream>
#include <string>
#include <vector>
using namespace std;
class User{
};
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
  • in short: use a `struct` / `class` – Karoly Horvath Feb 14 '14 at 18:20
  • Post a working example of what you tried. Your current code shouldn't compile, `fin != "end"` won't do what you think and I don't see `user_l` declared anywhere. Question is also unclear. – AliciaBytes Feb 14 '14 at 18:22
  • possible duplicate of [reading a line from ifstream into a string variable](http://stackoverflow.com/questions/6663131/reading-a-line-from-ifstream-into-a-string-variable). Also see http://stackoverflow.com/questions/13551911/read-text-file-into-string-c-ifstream. – jww Feb 14 '14 at 18:24
  • `while (fin != "end")` is an infinite loop... Assuming it even compiles in the first place... Pretty sure C++ doesn't define a useful comparison between `ifstream` and `const char const *`... – twalberg Feb 14 '14 at 19:49

1 Answers1

0

Three steps should resolve this:

  1. Make your User class have two string data members for the username and password.
  2. Create a stream extractor for User that extracts stream data into both the username and password
  3. Continually extract into a User object and push_back() a copy of that object to the vector.
David G
  • 94,763
  • 41
  • 167
  • 253