GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Feb-2008, 02:43
muhiuddin muhiuddin is offline
New Member
 
Join Date: Feb 2008
Posts: 4
muhiuddin is on a distinguished road

Find String In C++


I want to make a program of Account from user give input Id No. and Password and the program check whether the id exist or not if exist verify the password input by user. The record should be in a text file as below.
Use simple way using classes as i have not expert in C++. It is best that password will be in case sensitive

File is in multiple lines with tabs as below

Record.txt
Code:
ID Pass Income 3626 xYasd 50,000 554 89dd 100,000 6997 di2M 70,500 0125 msuu 1,500 6554 kDew 120,000
  #2  
Old 03-Feb-2008, 10:30
davis
 
Posts: n/a

Re: Find String In C++


Quote:
Originally Posted by muhiuddin
I want to make a program of Account from user give input Id No. and Password and the program check whether the id exist or not if exist verify the password input by user. The record should be in a text file as below.
Use simple way using classes as i have not expert in C++. It is best that password will be in case sensitive

File is in multiple lines with tabs as below

Record.txt
ID Pass Income
3626 xYasd 50,000
554 89dd 100,000
6997 di2M 70,500
0125 msuu 1,500
6554 kDew 120,000


...so, what is your question? I realize that you are also probably not an expert in English, but if you do not ask a question, what can be the answer?

Here is an example of a very quick hack. It may help you get started.

CPP / C++ / C Code:
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <vector>
#include <map>
#include <algorithm>

#include <cstdlib>
#include <ctime>
#include <cctype>

typedef std::vector<char> vChars;

class Password
{
public:
    const static unsigned int	DEFAULT_MIN_LENGTH	    = 8;
    const static unsigned int	DEFAULT_MAX_LENGTH	    = 16;
    const static unsigned int	DEFAULT_MIN_SPECIAL	    = 2;

    const static char		CHAR_START		    = 0x21;
    const static char		CHAR_END		    = 0x7F;

    Password() :
    m_min_length( DEFAULT_MIN_LENGTH ),
    m_max_length( DEFAULT_MAX_LENGTH ),
    m_length( DEFAULT_MIN_LENGTH ),
    m_min_special( DEFAULT_MIN_SPECIAL ),
    m_num_special( DEFAULT_MIN_SPECIAL ),
    m_seed( time(0) ),
    m_password( "" )
    {
	init();
    }
    Password( Password const& rhs )
    {
	m_min_length	= rhs.m_min_length;
	m_max_length	= rhs.m_max_length;
	m_length	= rhs.m_length;
	m_min_special	= rhs.m_min_special;
	m_num_special	= rhs.m_num_special;
	m_password	= rhs.m_password;
    }
    Password& operator=( Password const& rhs )
    {
	init();
	if( this != &rhs )
	{
	    m_min_length    = rhs.m_min_length;
	    m_max_length    = rhs.m_max_length;
	    m_length	    = rhs.m_length;
	    m_min_special   = rhs.m_min_special;
	    m_num_special   = rhs.m_num_special;
	    m_password	    = rhs.m_password;
	}
	return *this;
    }
    bool compare( Password const& rhs ) const
    {
	bool matches = false;
	if( m_password == rhs.m_password )
	{
	    matches = true;
	}
	return matches; 
    }
    friend std::ostream& operator<<( std::ostream& os, Password const& rhs )
    {
	os << rhs.m_password;
	return os;
    }
    friend std::istream& operator>>( std::istream& is, Password& rhs )
    {
	is >> rhs.m_password;
	return is;
    }
    std::string generate_random()
    {
	static unsigned int prev;
	if( prev == m_seed )
	{
	    ++m_seed;
	}
	srand( m_seed );
	random_shuffle( m_chars.begin(), m_chars.end() );
	random_shuffle( m_special.begin(), m_special.end() );
	std::string s;
	s.insert( s.end(), m_chars.begin(), m_chars.begin() + (m_length - m_num_special) );
	s.insert( s.end(), m_special.begin(), m_special.begin() + m_num_special );
	random_shuffle( s.begin(), s.end() );
	prev = m_seed;
	return s;
    }
    int get_min_length() const
    {
	return static_cast<int>( m_min_length );
    }
    void set_min_length( const int length )
    {
	if( length <= 0 )
	{
	    m_min_length = DEFAULT_MIN_LENGTH;
	}
	else
	{
	    unsigned int ui_length = static_cast<unsigned int>( length );
	    if( ui_length > 0 )
	    {
		if( ui_length <= m_max_length )
		{
		    m_min_length = ui_length;
		    m_length     = ui_length;
		}
	    }
	}
    }
    int get_max_length() const
    {
	return static_cast<int>( m_max_length );
    }
    void set_max_length( const int length )
    {
	if( length <= 0 )
	{
	    m_max_length = DEFAULT_MAX_LENGTH;
	}
	else
	{
	    unsigned int ui_length = static_cast<unsigned int>( length );
	    if( ui_length >= m_min_length )
	    {
		m_max_length = ui_length;
	    }
	}
    }
    int get_min_special() const
    {
	return m_min_special;
    }
    void set_min_special( const int length )
    {
	if( length >= 0 )
	{
	    m_min_special = length;
	    m_num_special = length;
	}
    }
    std::string get_password() const
    {
	return m_password;
    }
    bool set_password( std::string const& password )
    {
	bool valid = is_valid( password );
	if( valid )
	{
	    m_password = password;
	}
	return valid;
    }
    bool is_valid() const
    {
	return is_valid( m_password );
    }
    bool is_valid( std::string const& password ) const
    {
	bool	     is_valid = false;
	unsigned int count    = 0;
	if( password.length() <= m_max_length &&
            password.length() >= m_min_length )
	{
	    for( unsigned int ui = 0; ui < password.length(); ui++ )
	    {
		if( is_special( password[ui] ) )
		{
		    ++count;
		    if( count >= m_min_special )
		    {
			is_valid = true;
		    }
		}
	    }
	}
	return is_valid;
    }
protected:
private:
    unsigned int    m_min_length;
    unsigned int    m_max_length;
    unsigned int    m_length;

    unsigned int    m_min_special;
    unsigned int    m_num_special;

    vChars	    m_chars;
    vChars	    m_special;

    unsigned int    m_seed;
    std::string	    m_password;

    void init()
    {
	for( char c = CHAR_START; c < CHAR_END; c++ )
	{
	    if( isalpha( c ) )
	    {
		m_chars.push_back( c );
	    }
	    else
	    {
		m_special.push_back( c );
	    }
	}
	srand( m_seed );
	random_shuffle( m_chars.begin(), m_chars.end() );
	random_shuffle( m_special.begin(), m_special.end() );
    }
    bool is_special( char const& c ) const
    {
	bool special = false;
	for( unsigned int ui = 0; ui < m_special.size(); ui ++ )
	{
	    if( c == m_special[ui] )
	    {
		special = true;
		break;
	    }
	}
	return special;
    }
};

class Record
{
public:
    Record() :
    m_id( "" ),
    m_password(),
    m_income( "" )
    {
    }
    Record( Record const& rhs )
    {
	m_id	    = rhs.m_id;
	m_password  = rhs.m_password;
	m_income    = rhs.m_income;
    }
    Record& operator=( Record const& rhs )
    {
	if( this != &rhs )
	{
	    m_id	= rhs.m_id;
	    m_password  = rhs.m_password;
	    m_income    = rhs.m_income;
	}
	return *this;
    }
    friend std::ostream& operator<<( std::ostream& os, Record const& rhs )
    {
	os << rhs.m_id << "\t" << rhs.m_password << "\t" << rhs.m_income;
	return os;
    }
    friend std::istream& operator>>( std::istream& is, Record& rhs )
    {
	is >> rhs.m_id >> rhs.m_password >> rhs.m_income;
	return is;
    }
    std::string get_id() const
    {
	return m_id;
    }
    void set_id( std::string const& id )
    {
	m_id = id;
    }
    std::string get_password() const
    {
	return m_password.get_password();
    }
    bool set_password( std::string const& password )
    {
	return m_password.set_password( password );
    }
    std::string get_income()
    {
	return m_income;
    }
    void set_income( std::string const& income )
    {
	m_income = income;
    }
protected:
private:
    std::string	    m_id;
    Password	    m_password;
    std::string	    m_income;
};

typedef std::map<std::string, Record> map_records;

class SimpleRecordManager
{
public:
    SimpleRecordManager()
    {
    }
    SimpleRecordManager( std::string const& filename )
    {
	Record r;
	std::ifstream infile( filename.c_str() );
	if( infile.is_open() )
	{
	    while( 1 )
	    {
		infile >> r;
		if( !infile.bad() && !infile.eof() && !infile.fail() )
		{
		    m_map_records.insert( std::pair<std::string, Record>( r.get_id(), r ) );
		}
		else
		{
		    infile.close();
		    break;
		}
	    }
	}
    }
    bool find_by_id( std::string const& id, Record& result )
    {
	bool found = false;
	map_records::iterator it = m_map_records.find( id );
	if( it != m_map_records.end() )
	{
	    result = (*it).second;
	    found  = true;
	}
	return found;
    }
private:
    map_records	m_map_records;
};

int test_password()
{
    Password p;
    
    std::string s = "abcd";
    std::cout << p.is_valid( s ) << std::endl;
    s = "abcdef$%";
    std::cout << p.is_valid( s ) << std::endl;
    
    p.set_max_length( 18 );
    p.set_min_length( 10 );
    p.set_min_special( 3 );

    std::cout << "Enter a file name to write passwords to: ";
    std::string filename;
    std::getline( std::cin, filename );
    std::ofstream outfile( filename.c_str() );
    if( outfile.is_open() )
    {
	for( int i = 0; i < 10; i++ )
	{
	    outfile << p.generate_random() << std::endl;
	}
	outfile.close();
    }

    std::ifstream infile( filename.c_str() );
    if( infile.is_open() )
    {
	while( 1 )
	{
	    infile >> p;
	    if( !infile.bad() && !infile.eof() && !infile.fail() )
	    {
		std::cout << p << std::endl;
	    }
	    else
	    {
		infile.close();
		break;
	    }
	}
    }
    return 0;
}

int test_record()
{
    Record r;

    std::cout << "Enter a file name from which read records: ";
    std::string filename;
    std::getline( std::cin, filename );
    std::ifstream infile( filename.c_str() );
    if( infile.is_open() )
    {
	while( 1 )
	{
	    infile >> r;
	    if( !infile.bad() && !infile.eof() && !infile.fail() )
	    {
		std::cout << r << std::endl;
	    }
	    else
	    {
		infile.close();
		break;
	    }
	}
    }
    return 0;
}

int test_simple_record_manager()
{
    std::cout << "Enter a file name from which read records: ";
    std::string filename;
    std::getline( std::cin, filename );

    SimpleRecordManager mgr( filename );
    std::vector<std::string> v;
    std::string s = "3626";
    v.push_back( s );
    s = "554";
    v.push_back( s );
    s = "6997";
    v.push_back( s );
    s = "0125";
    v.push_back( s );
    s = "6554";
    v.push_back( s );
    s = "bogus";
    v.push_back( s );

    bool   found;
    Record r;
    for( unsigned int ui = 0; ui < v.size(); ui++ )
    {
	found = mgr.find_by_id( v[ui], r );
	std::cout << "id " << v[ui] << " found = " << found << std::endl;
	if( found )
	{
	    std::cout << r << std::endl;
	}
    }
    return 0;
}

int main()
{
    test_simple_record_manager();
    return 0;
}


Output:

Code:
$ cat /tmp/records.txt 3626 xYasd 50,000 554 89dd 100,000 6997 di2M 70,500 0125 msuu 1,500 6554 kDew 120,000 id 3626 found = 1 3626 xYasd 50,000 id 554 found = 1 554 89dd 100,000 id 6997 found = 1 6997 di2M 70,500 id 0125 found = 1 0125 msuu 1,500 id 6554 found = 1 6554 kDew 120,000 id bogus found = 0

...note that I ran out of time before having an opportunity to implement validating reads on the password based on its validation interfaces. If necessary, you should be able to handle that easily enough from the code I've provided.

Also, this was a very quick hack thrown together to hopefully illuminate you as to what is at least one attempt at an implementation using basic C++ and not C++ as a better C. I hope that it meets your base requirements. If you find bugs or any errors in the logic, they are the result of too quickly writing this code and too little testing of it.

You'll also note that I removed the first line (the header) of the text from your "records file." It should not be too difficult for you to modify the code to skip that line if it occurrs in the "real" input file.


:davis:
 
 

Recent GIDBlog2nd Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Read a .html file, check that file for links salemite C Programming Language 10 17-Jan-2008 07:56
Multiple questions for C++ project devster420 CPP / C++ Forum 1 20-Apr-2007 21:26
Message Class TransformedBG CPP / C++ Forum 5 29-Nov-2006 21:28
Help wit my source code compiler errors Krandygrl00 CPP / C++ Forum 1 06-Jun-2005 08:14

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:17.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.