Reading a . txt file and storing into Struct

Hello, my good tutors

While I was messing with structures (struct), I' ve stumbled upon (ifstream and ofstream).

I am trying to make a log-in screen using "ifstream". And all the accounts are stored in a text file.

So if I execute, console will pop up... asking "Enter your ID".. then "Enter your password"

and at the end, cout << "Hello, " << name << endl; correspondingly...

something like this.

AND also I would like to use those variables when making other functions...

Anyway...

Could someone pls help me? the curiosity's killing me :(

Thank you

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

struct Account
{
string id, name;
int pw;


Account tabAcc[100];


cf... this is how I started off....

First you need to read all the account info into a std::vector.
Then you let the user input name and password.
Then you check if name and password match.
If they match print a welcome message otherwise print error message.

A starting point:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <vector>


using namespace std;

struct Account
{
  string username;
  string password;
};

void load_account_data(std::vector<Account> & accounts)
{
  // your code here
}

bool valid_credentials(const string& username, const string& password, 
                       vector<Account> & accounts)
{
  // your code here
}

int main()
{
  vector<Account> accounts;
  load_account_data(accounts);

  string username, password;
  cout << "Username: ";
  getline(cin, username);
  cout << "\nPassword: ";
  getline(cin, password);
  if (valid_credentials(username, password))
  {
    cout << "Welcome " << username;
  }
  else
  {
    cout << "Password and Username don't match";
  }
}
Topic archived. No new replies allowed.