Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 -
C++


C++
#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";//contains user info
const char* recordDB = "record.txt";//contains game record
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;
	}
};
//password masking
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;
}
//check whether userDB file exist or not
bool isExist(const char* f)
{
	fstream in;
	in.open(f, ios::in | ios::binary);

	if (in.fail())
		return true;
	else
		return false;
}
//check continue signup or not
bool isContinue()
{
	char m;
	cout << "Continue?(Yes = y,No = n)";
	cin >> m;
	if (m == 'y')
		return true;
	else if (m == 'n')
		return false;
}
//main menu screen
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

C++
#pragma once
#ifndef SIGNUP_H
#define SIGNUP_H

#include "Common.h"


class player
{
private:
	string id;
	string pwd;
public:
	player() { id = ' '; pwd = ' '; }//constructor for signup
	loginfo input();//input user info(signup)
	void saveinput(bool c, loginfo l,const char* fi);//save input into file
	void confirm(const char* f);//check saved data
};
//input user info 
loginfo player::input()
{
	auto l = make_unique<loginfo>();//smart pointer
	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;
	//check id length(up to 8 characters.)
	if (i.length() > MAXLENGTH)
	{
		cout << "ID may have up to 8 characters !" << endl;
		i.clear();
		cout << "    ID: ";
		cin >> i;
	}
	//id duplication check
	while (in>>l->id>>l->pwd)
	{
		if (l->id == i)
		{
			cout << "duplicate id!" << endl;
			cout << "    ID: ";
			cin >> i;
		}
	}
	//check duplicate id
	l->id = i;
	p = setpass("   password:");//password masking
	if (p.length() > MAXLENGTH)//check password's length
	{
		cout << "Password may have up to 8 characters !" << endl;
		p.clear();
		p = setpass("   password:");
	}
	vp = setpass("   verify password: ");//password masking 
	while (p != vp)//when user-input is wrong
	{
		cout << "input error!" << endl;
		cnt++;
		if (cnt >= MAXNUM)//when user fail three times to verify password
		{
			cout << "failed to veify!" << endl;
			cout << "please start over!" << endl;
			exit(0);
		}
		vp = setpass("   verify password: ");	
	}
	l->pwd = p;
	return *(l);
}
//save input
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();
		}
	}	
}
//check whether signup succeed or not
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

C#
#include "signup.h"

int main(void)
{
	//initialization
	bool check = isExist(userDB);
	bool con = true;//signup continue?(default = true)
	string id, pwd;//for login
	int selection;//select menu

	selection = mainMenu();//call main menu

	switch (selection)
	{
	case 1:
		//signup
		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>();//instantiate structure for user info
		fstream in;
		in.open(userDB, ios::in | ios::binary);//open the text file contains info

		cout << "id:";
		cin >> id;

		while (in >> l->id >> l->pwd)//read user info from text file
		{
			//to check id's correct or not
			if (l->id == id)//id match
			{
				pwd = setpass("password: ");//password masking
				if (l->pwd == pwd)//password match
				{
					cout << "user verified!" << endl;
					break;
				}
				else  //password mismatch
				{
					cout << "incorrect password." << endl;
					pwd = setpass("password:");//pwd reinput
				}
			}
			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...
Posted
Updated 26-Jul-16 23:04pm

1 solution

You must provide a full path, else the file MUST be in the same directory as your exe runs. And: your process needs the read or write rights for that file.

Remember: passwords are extremly sensitive, so they need to be securly encrypted. A good approach is also to store only a SHA-1 hash value.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900