Dear guys,
I'm writing a code which validates id and password from plain-text file(user.txt)
i wanted to make program check id after i typed it by reading data from user.txt but I had a problem.
example:
ID: louis
pwd:lenox
incorrect id!
these id and pwd surely is in user.txt but my program couldn't read it.
I'll show my code to you and someone pick up the error in my code to fix it, I would appreciate it very much.
- header file -
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#ifndef COMMON_H
#define COMMON_H
#include <iostream>
#include <memory>
#include <fstream>
#include <string>
#include <iomanip>
#include <conio.h>
using namespace std;
const char* userDB = "user.txt";const char* recordDB = "record.txt";const char* help = "help.txt";
#define MAXLENGTH 8
#define MAXNUM 3
#define MAXTRIALS 3
struct loginfo
{
string id;
string pwd;
friend istream &operator >> (istream& is, loginfo l)
{
return is >> l.id >> l.pwd;
}
};
string setpass(const char* prompt, bool show_asterisk = true)
{
const char BACKSPACE = 8;
const char RETURN = 13;
string pass;
unsigned char c = 0;
cout << prompt;
while ((c = _getch()) != RETURN)
{
if (c == BACKSPACE)
{
if (pass.length() != 0)
{
if (show_asterisk)
cout << "\b \b";
pass.resize(pass.length() - 1);
}
else if (c == 0 || c == 224)
{
_getch();
continue;
}
}
else
{
pass.push_back(c);
cout << '*';
}
}
cout << endl;
return pass;
}
bool isExist(const char* f)
{
fstream in;
in.open(f, ios::in | ios::binary);
if (in.fail())
return true;
else
return false;
}
bool isContinue()
{
char m;
cout << "Continue?(Yes = y,No = n)";
cin >> m;
if (m == 'y')
return true;
else if (m == 'n')
return false;
}
int mainMenu()
{
int ch;
cout << "================" << endl;
cout << " Welcome!!! " << endl;
cout << " 1. signup " << endl;
cout << " 2. login " << endl;
cout << " 3. record " << endl;
cout << " 4. help " << endl;
cout << " 5. exit " << endl;
cout << "================" << endl;
cout << "your choice:";
cin >> ch;
return ch;
}
#endif
#pragma once
#ifndef SIGNUP_H
#define SIGNUP_H
#include "Common.h"
class player
{
private:
string id;
string pwd;
public:
player() { id = ' '; pwd = ' '; } loginfo input(); void saveinput(bool c, loginfo l,const char* fi); void confirm(const char* f);};
loginfo player::input()
{
auto l = make_unique<loginfo>(); string i,p,vp;
int cnt = 0;
fstream in;
in.open(userDB, ios::in|ios::binary);
cout << "====================" << endl;
cout << " signup " << endl;
cout << " ID: ";
cin >> i;
if (i.length() > MAXLENGTH)
{
cout << "ID may have up to 8 characters !" << endl;
i.clear();
cout << " ID: ";
cin >> i;
}
while (in>>l->id>>l->pwd)
{
if (l->id == i)
{
cout << "duplicate id!" << endl;
cout << " ID: ";
cin >> i;
}
}
l->id = i;
p = setpass(" password:"); if (p.length() > MAXLENGTH) {
cout << "Password may have up to 8 characters !" << endl;
p.clear();
p = setpass(" password:");
}
vp = setpass(" verify password: "); while (p != vp) {
cout << "input error!" << endl;
cnt++;
if (cnt >= MAXNUM) {
cout << "failed to veify!" << endl;
cout << "please start over!" << endl;
exit(0);
}
vp = setpass(" verify password: ");
}
l->pwd = p;
return *(l);
}
void player::saveinput(bool c, loginfo l,const char* fi)
{
fstream f;
if (c == true)
{
f.open(fi, ios::out | ios::binary);
if (!f)
{
cout << "failed to open!" << endl;
exit(0);
}
else
{
cout << "saving..." << endl;
f << l.id << " " << l.pwd << endl;
cout << "registration complete." << endl;
f.close();
}
}
else
{
f.open(fi, ios::out | ios::app | ios::binary);
if (!f)
{
cout << "failed to open!" << endl;
exit(0);
}
else
{
cout << "appending..." << endl;
f << l.id << " " << l.pwd << endl;
cout << "successfully appended." << endl;
f.close();
}
}
}
void player::confirm(const char* f)
{
loginfo l;
fstream in;
in.open(f, ios::in | ios::binary);
if (!in)
{
cout << "failed to read!" << endl;
exit(0);
}
while (in >> l.id >> l.pwd)
;
cout << "player info: " << endl;
cout << "Id: " << l.id << "," << "Password:" << l.pwd << endl;
}
void showAll()
{
auto l = make_unique<loginfo>();
fstream in;
in.open(userDB, ios::in);
while (in>>l->id>>l->pwd)
{
cout << l->id << " " << l->pwd << endl;
}
}
#endif
#include "signup.h"
int main(void)
{
bool check = isExist(userDB);
bool con = true;
string id, pwd;
int selection;
selection = mainMenu();
switch (selection)
{
case 1:
while (con)
{
auto p = make_unique<player>();
loginfo ll;
ll = p->input();
p->saveinput(check, ll, userDB);
p->confirm(userDB);
con = isContinue();
}
break;
case 2:
auto l = make_unique<loginfo>();
fstream in;
in.open(userDB, ios::in | ios::binary);
cout << "id:";
cin >> id;
while (in >> l->id >> l->pwd)
{
if (l->id == id)
{
pwd = setpass("password: ");
if (l->pwd == pwd)
{
cout << "user verified!" << endl;
break;
}
else
{
cout << "incorrect password." << endl;
pwd = setpass("password:");
}
}
else
{
cout << "incorrect id" << endl;
}
}
break;
}
system("pause");
return 0;
}
What I have tried:
I wrote a code to test my data file's integrity as follows;
auto l = make_unique<loginfo>();
fstream in;
in.open(userDB, ios::in | ios::binary);
cout << "id:";
cin >> id;
while (in >> l->id >> l->pwd)
{
if (l->id == id)
cout << l->id << " " << l->pwd << endl;
}
this code perfectly worked as i intended. this made me very confused...