Tuesday, May 18, 2010

Phone Number List

// Programming Environment: Bloodshed
// Program Description:
   // This program asks the user to input the name or partial name and it
   // searches the database in a file to give the user with the record that
   // contains some or all of the records that was matched during the search
   // process. It takes input from a file and transfers the information to an
   // array and performs its search operation using a function. This function
   // basically converts both the user input and the input from the file to
   // lower case so that it wouldn't have an effect on the search. When the
   // search actually find the records then it again displayes the record that
   // was not converted to lowercase. Hence, it uses smart techniques to search
   // the names and its corresponding phone numbers to act as a Phone Diary.
//------------------------------------------------------------------------------        
// Date/Time: 4/12/2010  2:42:29 AM
//------------------------------------------------------------------------------
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "extras.h"
#include "Generic_Class.h"
using namespace std;
//---------------------------------------------------------------------------
// Named Constants
//---------------------------------------------------------------------------
const int NAME_SIZE = 50; // Setting name size for user input.
const char INPUT_PATH_FILE[] =
    "C:\\c++ 242\\Assignments\\assignment3\\assign3_with_functions\\phone_num.txt";
    // Path for the database to be accessed by the programmer.
const int LENGTH = 256; // Setting database record limit to 256 records.
const int WIDTH = 50; // Setting each record length limit 50.
//---------------------------------------------------------------------------
// Structure Declarations
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// Function Prototypes
//---------------------------------------------------------------------------
void SearchList(char[][WIDTH], int, char[]); // To Search name out of database.
bool Search_Again (void);
//---------------------------------------------------------------------------
int main (void) {

    cout.setf (ios::fixed, ios::floatfield);
    cout.setf (ios::showpoint);
    cout.precision (2);

    do {  
  
    char name [NAME_SIZE];
    cout << endl << endl << "PHONE DIARY Ver. 1.3";
    cout << endl << endl << "Search: ";
    cin.getline (name, NAME_SIZE); // Getting the input from the user.
    for (int j = 0; j < strlen (name); ++j) {
        name[j] = tolower (name[j]); // Converting input from user to lower case.
    }
        
    ifstream InFile; // Input file stream.
    InFile.open (INPUT_PATH_FILE); // Opening the file.
    if (InFile) { // Performing operations if the input file is open.
        // Input file opened successfully.      
        char people_information[LENGTH][WIDTH]; // Creating 2-D Array.            
        int count = 0;
        // Creating integer variable so that we could store the number of
        // records there are in our file. If the user inputs more data to the
        // file then we could keep track of how many records are there in our file.                
        while(InFile.getline(people_information[count], WIDTH)) {
            // Reading records in file till the end of the file.        
            ++ count; // Counting the records.                          
        }        
        InFile.close();
        // Closing the file as we have already transfered the records from file
        // to 2-D Array called people_information[][].
      
        cout << endl << "Database Contains " << count  << " Records!!" << endl;
        cout << "------------------------------" << endl << endl;;
        // Informing user the number of records that the notepad file contains!  
      
        SearchList(people_information, count, name);
        // Invoking the function SearchList() to search the name or partial name
        // to get the full name or the phone number out of the database in
        // notepad file.    
                  
    } else {
        cout << endl << "\aCannot Open Input File";    
    }
      
} while (Search_Again ());
    return 0;
}
//------------------------------------------------------------------------
// Function Definitions
//------------------------------------------------------------------------
bool Search_Again (void) {

    (void) fflush (stdin);
    cout << endl << endl << "\aSearch Again? (y/n): ";
    return static_cast(tolower (_getch ())) == 'y';
}

void SearchList(char people_information[][WIDTH], int count, char name[]){
     // This funtion defination takes the information stored in array called
     // people_information[], copies it to a duplicate array called
     // Duplicate_Array[] by converting it to lower case so that when user
     // performs search operation then it won't affect whether they enter lower
     // case or upper case name or partial name.
  
     char Duplicate_Array[LENGTH][WIDTH]; // Creating Duplicate Array.              
     for (int i = 0; i < count; ++i) {
         // Performs loop for number of records we have in our database. If the
         // database is updated and user adds another record in the phone book
         // then this loop would update itself to loop that many times.
         strcpy(Duplicate_Array[i], people_information[i]);
         // Copies all the information from array people_information[] to
         // Duplicate_Array[].                          
         for (int j = 0; j < count; ++j) {
             // Converts the elements of Duplicate_Array[][] to lower case
             // First it converts all the elements of row 1  to lower case and
             // so on  untill it reaches the last records of the array.                                                          
             Duplicate_Array[i][j] = tolower(Duplicate_Array[i][j]);          
         }
     }
              
     bool found = false; // Setting found as false.
     char* strptr; // Creating a character pointer.
     for (int i = 0; i < count; ++i) {  
         // Searches the whole record to find the match.
         strptr = strstr(Duplicate_Array[i], name);
         // Points to the address where the name is matched to the user input.        
         if(strptr) {
              // If the strptr is true then it displays the record that is found.
              // Hence, we now only write people_information[i] so that only that
              // record is displayed and the original record that is not
              // converted to lower case is displayed.
              cout << endl << "Found: " << people_information[i] << endl;      
              found = true; // Setting found as true as we find record.        
         }
     }
          
     if(!found) {
         // If the record is not found then bool variable is set to false and
         // if its true then we inform the user that the record in not found.
         cout << endl << "Data Not Found!!";
         cout << endl << "Add This Data In File Named Phone_Num.txt";
     }
     delete strptr;
     // Deleting the strptr pointer as it has no use now at this
     // point of the program.
}




This is the text file named "phone_num.txt" looks like,

Becky Warren, 555-1223
Joe Looney, 555-0097
Geri Palmer, 555-8787
Lynn Presnell, 555-1212
Holly Gaddis, 555-8878
Sam Wiggins, 555-0998
Bob Kain, 555-8712
Tim Haynes, 5557676
Warren Gaddis, 555-9037
Jean James, 555-4939
Ron Palmer, 555-2783
Praj Shrestha, 720-233-2603
James Bond, 777-888-7777
Raj, 232-232-2442
Ram, 3234232
James Bond, 777-777-7777
Collen Farrel, 720-234-6421
Winnona Raider, 303-223-1023
Ujjwal Shrestha, 302-332-1234

1 comment:

  1. Please put your mess in a code format. It is hard to read.

    ReplyDelete

Note: Only a member of this blog may post a comment.