Thursday, December 13, 2012

C++ String Manipulation functions: Card Verification System - Examples of String append & Substring, Reading database file


/*
PROGRAM: String Manipulation functions - Examples of String append and Substring
PURPOSE: The user is prompted to input a card number with 16 digits (four numbers
         at a time, starting from digits one to four), then the program uses the
         string append function to connect these series of four numbers to make
         up a string of 16 numbers. This card number is then matched against a
         card database file using the substring function to extract the first 16
         characters within the record then compared to the user input. If a match
         is found, the program returns a message "Card verification successful!",
         otherwise, it will display a "Card not found on database" message on the
         screen.
LANGUAGE: C++
AUTHOR: eternaltreasures
DATE: 2010 October 3
*/


#include<iostream>
#include <string>
#include<fstream>
using namespace std;

#define newline '\n'

string card_database_record;

string card_database_record_substring;

string string4; 
string card_input_record;

bool cardfound = false;

int database_record_index = 0;

void input_card_number ()
{
 cout << newline;
 cout << "Welcome to the Secure Card Verification System!";
 cout << newline;
 cout << newline;
 cout << "Enter your 16-digit card number:";
 cout << newline;
 cout << newline;

 cout << "1st to 4th digit: "; cin >> string4;
 card_input_record.append (string4);
 database_record_index = database_record_index + 4;

 cout << "5th to 8th digit: "; cin >> string4;
 card_input_record.append (string4);
 database_record_index = database_record_index + 4;

 cout << "9th to 12th digit: "; cin >> string4;
 card_input_record.append (string4);
 database_record_index = database_record_index + 4;

 cout << "13th to 16th digit: "; cin >> string4;
 card_input_record.append (string4);
 database_record_index = database_record_index + 4;

}

void compare_match_input ()
{
  
     card_database_record_substring = card_database_record.substr (0, database_record_index);
     
    
     if (card_input_record == card_database_record_substring)
     {
      cout << "Card "  << card_input_record << ": Card verification successful!";
     cardfound = true;}
    



void read_database_file ()
{
ifstream card_database_file;

card_database_file.open ("database.txt");

if (card_database_file.is_open())
{
 while (!card_database_file.eof())
  {
   card_database_file >> card_database_record;
 cout << endl;
   compare_match_input ();
  
   if (cardfound = false)
  
     cout << "End of file: Card not found on database.";
  }
}
else
{ cout << "Input file not successfully opened.";
card_database_file.close();
}
}


int main()
{
input_card_number ();
read_database_file ();

return 0;
}

No comments:

Post a Comment