Can someone help me on how to write the program?

The details i already upload.See the attachment.The due date is tomorrow,i hope someone can help me.Thanks!

Recommended Answers

All 7 Replies

>The due date is tomorrow
And you were given the assignment today, I'm sure. :icon_rolleyes: You seem like yet another boob who screwed around when he should have been working and expects us to provide a free lunch.

You want help? Show us your code. If it looks like you've actually been working on this assignment, you'll appear less like a hopeless leech.

Here is my code.But some function does not working.And i dun noe how to make the date and time automatically updated.???

//Header Files
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

//File Name Definition
char fname[] = "data.txt";
char fswap[] = "swap.txt";

//Main Menu Function Declare
void mainmenu();

//Misc Function Declare
void empty(FILE *x, char y[],char z[]);
void change(FILE *x, char y[],char z[]);
void credit();


//Search Function Declare
void search();
void s_name();

//Add Functions Declare
void add();

//Delete Function Declare
void delete();
void d_name();

//Edit Function Declare
void edit();
void e_name();
void e_email();
void e_phonenumber();
void e_dateupdated();

//Display Options Declare
void display_options();

//Structure Data Declare
struct data
{
    char name[50];
    char email[20];
    char phonenumber[30];
    char date_updated[30];
}record;

//Global Variable Declare
long sum=0;

int main()
{
    credit();
    mainmenu();
}

/* Main Menu Generated */
void mainmenu()
{
    char a[1];
    int c=1;
    while(c!=-1)
    {
           system("cls"); 
           printf("Personal Address Book");
           printf("\n");
           printf("\n");
           printf("What you want to do\n");
           printf("\n");
           printf("a. Search Record");
           printf("\n");
           printf("b. Add Record");
           printf("\n");
           printf("c. Delete Record");
           printf("\n");
           printf("d. Edit Record");
           printf("\n");
           printf("e. Display Options");
           printf("\n");
           printf("f. Exit");
           printf("\n");
           printf("\n");
           
           if ( c < 1|| c > 6 )
           {
                printf("!!!!Please Enter a-f!!!!\n");
                
           }
           printf("Choose An Option:");
           scanf("%s", a);
           
           if(isdigit(a[0]))    //Determine The Entered Value
           c = atoi (a);
           else
           c = -2;
           
           
           switch (c)
           {
            case 1: search();                                  break;  //Goto Search Record Function
            case 2: add();                                     break;  //Goto Add Record Function
            case 3: delete();                                  break;  //Goto Delete Record Function
            case 4: edit();                                    break;  //Goto Edit Record Function
            case 5: display_options();                         break;  //Goto Display Options
            case 6: exit(1);                                   break;  //Exit Program
            default:                                           break;  //Loop Main()
           }
    }
} 

      
/*
Add Record Function
===================
Insert Name,Email,Phone Number
Save Into File Stream
*/
void add()
{
    system("cls");
    FILE *fp;
    int a=1;

    fp = fopen(fname,"ab");
    if(fp!=NULL)
    {
        while ( a == 1 )
        {
            system("cls");
            printf("<<----Adding New Record---->>");
            printf("\n");
            printf("Enter first Name:\n");
            scanf("%s", record.name);
            printf("Enter Last Name:\n");
            scanf("%s", record.name);
            printf("Enter Email:\n");
            scanf("%s", record.email);
            printf("Enter Phone Number:\n");
            scanf("%s", record.phonenumber);
                                 
            fwrite(&record, sizeof(record), 1, fp);
            
            printf("Record Saved\n");
            printf("DO YOU WISH TO CONTINUE..[1]/[0]: \n");
            scanf("%1d", &a);
            if( a == 0)
            break;
        }
        fclose(fp);
    }
    else
    {
        printf("Unable To Open/Create File...");
        system("pause");
    }
}

/*
Search Record Function
====================
Generate Searching Option Menu
Search Record by Name
*/
void search()
{
    char a[1];
    int c=1;
    while(c!=-1)
    {
        system("cls");
        puts("a. Search by Name");
        puts("b. Back");
        puts("");
        if ( c < 0 || c > 1)
        {
            printf("!!!!Please Enter a-b!!!!\n");
        }
        printf("Choose an Option: ");
        scanf("%s", a);
        
        if(isdigit(a[0]))   //Determine The Entered Value
        c = atoi (a);
        else
        c = -2;
        
        switch (c)
        {
            case 1: s_name();    break;  //Goto Search by Name
            case 0: mainmenu();  break;  //Return Main Menu
            default:             break;  //Loop Search()
        }
    }
}

/*
Search by Name Function
===================================
Read Through File Stream
Insert Search String
String Comparison between Search String and Name
Retrieve Data for String Comparison is True
Print Out All Retrieved Data
*/
void s_name()
{
    int a;
	int ch;
	FILE *fp;
	char check[10];
	system("cls");

	fp=fopen(fname,"rb+");
	
	if (fp!=NULL)
	{
    	printf("Please Enter Search String: ");
    	scanf("%s", check);
    	
        while ( fread(&record, sizeof(record), 1, fp) && strcmp(check,record.name) );
    
    	ch=strcmp(check,record.name);
    	if(ch!=0)
    	{
            system("cls");
            printf("==SEARCHING==");
            printf("\n");
            system("pause");
            system("cls");
    		printf("==RECORD NOT FOUND==\n");
    	}
    	else
    	{
            system("cls");
            printf("==SEARCHING==");
        	printf("\n");
        	system("pause");
        	system("cls");
        	printf("records found:\n");
        	printf("%s\n\%s\n\%2s\n\%s\n","Name:","Email:","Phone number:","Date Updated:");
          	for ( a = 0; a<=78; a++)
            printf("=");
            printf("\n");
            rewind(fp);
            while ( fread(&record, sizeof(record), 1, fp))
            {
                if (!strcmp(check,record.name))
            	printf("%s\n\%s\n\%2s\n\%s\n", record.name, record.email, record.phonenumber, record.date_updated);
            }
        }
        fclose(fp);
    	system("pause");
    }
    else
        {
            printf("Unable To Open File...\n");
            system("pause");
        }
}

/*
Delete Record Function
======================
Generate Delete Option Menu
Delete Record by Searhing Name
*/
void delete()
{
    char a[1];
    int c=1;
    while(c!=-1)
    {
        system("cls");
        puts("[1] Delete by Name");
        puts("[0] Back");
        puts("");
        if ( c < 1 || c > 0)
        {
            printf("!!!!Please Enter 0-1!!!!\n");
        }
        printf("Choose an Option: ");
        scanf("%s", a);
        
        if(isdigit(a[0]))   //Determine The Entered Value
        c = atoi (a);
        else
        c = -2;
        
        switch (c)
        {
            case 1: d_name();    break;  //Goto Delete by Name
            case 0: mainmenu();  break; //Return Main Menu
            default:             break;  //Loop Del()
        }
    }
}

/*
Delete by Name
========================
Read Through File Stream
Insert Search String
String Comparison between Search String and Record Name
Retrieve Data for String Comparison is True
Print Out Data
Flag For Delete The Printed Data
    if true Clear the Record by Goto empty()
    if false Return Back Delete Menu Function
*/
void d_name()
{
    int a;
	int ch;
	FILE *fp;
	char check[10];
	int flag;
	system("cls");

	fp=fopen(fname,"rb+");
	
	if(fp!=NULL)
	{
    	printf("Please Enter Search String: ");
    	scanf("%s", check);
    
    		while ( fread(&record, sizeof(record), 1, fp) && strcmp(check,record.name) );
    
    			ch=strcmp(check,record.name);
    				if(ch!=0)
    				{
    					printf("==RECORD NOT FOUND==\n");
    					system("pause");
    				}
    				else
    				{
                        system("cls");
    					printf("==SEARCH SUCCESSFUL==\n");
    					system("pause");
    					system("cls");
    				    printf("%s%10s%20s%20s\n","Name","Email","Phone Number","Date Updated");
                        for ( a = 0; a<=78; a++)
                        printf("=");
                        printf("\n");
                        rewind(fp);
                        while ( fread(&record, sizeof(record), 1, fp))
                        {
                            if (!strcmp(check,record.name))
        	                   printf("%s%10s%20s%20s\n", record.name, record.email, record.phonenumber, record.date_updated);
                        }
    					printf("Delete this Record [1]/[0]: ");
    					scanf("%1d", &flag);
    					if (flag == 1)
                        {
                            empty(fp,check, record.name);
    					    printf("RECORD DELETED !!!\n");
    					    system("pause");   
                        }
                        else
                        return;
    				}
    }
    else
        {
            printf("Unable To Open File...\n");
            system("pause");
        }
}

/*
Edit Record Function
======================
Generate Edit Option Menu
Edit Record by Searhing Name, Email, Phone Number, Date Updated
*/
void edit()
{
    char a[1];
    int c=1;
    while(c!=-1)
    {
        system("cls");
        puts("[1] Edit Name");
        puts("[2] Edit Email");
        puts("[3] Edit Phone number");
        puts("[0] Back");
        puts("");
        if ( c < 0 || c > 4)
        {
            printf("!!!!Please Enter 0-3!!!!\n");
        }
        printf("Choose an Option: ");
        scanf("%s", a);
        
        if(isdigit(a[0]))   //Determine The Entered Value
        c = atoi (a);
        else
        c = -2;
        
        switch (c)
        {
            case 1: e_name();         break;  //Goto Edit by Name
            case 2: e_email();        break;  //Goto Edit by Email
            case 3: e_phonenumber();  break;  //Goto Edit by PhoneNumber
            case 0: mainmenu();       break; //Return Main Menu
            default:                  break;//Loop Edit()
        }
    }
}

/*
Edit by Name Function
=================================
Read Through File Stream
Insert Search String
String Comparison between Search String and Record Name
Retrieve Data for String Comparison is True
Print Out Data
Flag For Edit The Printed Data
    if true Clear the Record by Goto change()
    if false Return Back Edit Menu Function
*/
void e_name()
{
    int a;
	int ch;
	FILE *fp;
	char check[10];
	int flag;
	system("cls");

	fp=fopen(fname,"rb+");
	
	printf("Please Enter Search String: \n");
	scanf("%s", check);

		while ( fread(&record, sizeof(record), 1, fp) && strcmp(check,record.name) );

			ch=strcmp(check,record.name);
				if(ch!=0)
				{
					printf("==RECORD NOT FOUND==\n");
					system("pause");
				}
				else
				{
                    system("cls");
					printf("==SEARCH SUCCESSFUL==\n");
					system("pause");
					system("cls");
				    printf("%s%9s%10s%20s\n","Name","","Email","Phonenumber","DateUpdated");
                    for ( a = 0; a<=78; a++)
                    printf("=");
                    printf("\n");
					printf("%5.5s%9.8s%10.9s%20.19s\n", record.name, record.email, record.phonenumber, record.date_updated);
					printf("Edit this Record [1]/[0]: ");
					scanf("%1d", &flag);
					if (flag == 1)
                    {
                        change(fp,check, record.name);
					    printf("RECORD Edited !!!\n");
					    system("pause");   
                    }
                    else
                    return;
				}
}

/*
Edit by Email Function
=========================
Read Through File Stream
Insert Search String
String Comparison between Search String and Record Email
Retrieve Data for String Comparison is True
Print Out Data
Flag For Edit The Printed Data
    if true Clear the Record by Goto change()
    if false Return Back Edit Menu Function
*/
void e_email()
{
    int a;
	int ch;
	FILE *fp;
	char check[10];
	int flag;
	system("cls");

	fp=fopen(fname,"rb+");
	
	printf("Please Enter Search String: \n");
	scanf("%s", check);

		while ( fread(&record, sizeof(record), 1, fp) && strcmp(check,record.email) );

			ch=strcmp(check,record.email);
				if(ch!=0)
				{
					printf("==RECORD NOT FOUND==\n");
					system("pause");
				}
				else
				{
                    system("cls");
					printf("==SEARCH SUCCESSFUL==\n");
					system("pause");
					system("cls");
				    printf("%s%9s%10s%20s\n","Name","Email","PhoneNumber","DateUpdated");
                    for ( a = 0; a<=78; a++)
                    printf("=");
                    printf("\n");
					printf("%5.5s%9.8s%10.9s%20.19s\n", record.name, record.email, record.phonenumber, record.date_updated);
					printf("Edit this Record [1]/[0]: ");
					scanf("%1d", &flag);
					if (flag == 1)
                    {
                        change(fp,check, record.email);
					    printf("RECORD Edited !!!\n");
					    system("pause");   
                    }
                    else
                    return;
				}
}

/*
Edit by PhoneNumber Function
=================================
Read Through File Stream
Insert Search String
String Comparison between Search String and Record PhoneNumber
Retrieve Data for String Comparison is True
Print Out Data
Flag For Edit The Printed Data
    if true Clear the Record by Goto change()
    if false Return Back Edit Menu Function
*/
void e_phonenumber()
{
    int a;
	int ch;
	FILE *fp;
	char check[10];
	int flag;
	system("cls");

	fp=fopen(fname,"rb+");
	
	printf("Please Enter Search String: \n");
	scanf("%s", check);

		while ( fread(&record, sizeof(record), 1, fp) && strcmp(check,record.phonenumber) );

			ch=strcmp(check,record.phonenumber);
				if(ch!=0)
				{
					printf("==RECORD NOT FOUND==\n");
					system("pause");
				}
				else
				{
                    system("cls");
					printf("==SEARCH SUCCESSFUL==\n");
					system("pause");
					system("cls");
				    printf("%s%9s%10s%20s\n","Name","Email","PhoneNumber","DateUpdated");
                    for ( a = 0; a<=78; a++)
                    printf("=");
                    printf("\n");
					printf("%5.5s%9.8s%10.9s%20.19s\n", record.name, record.email, record.phonenumber, record.date_updated);
					printf("Edit this Record [1]/[0]: ");
					scanf("%1d", &flag);
					if (flag == 1)
                    {
                        change(fp,check, record.phonenumber);
					    printf("RECORD Edited !!!\n");
					    system("pause");   
                    }
                    else
                    return;
				}
}

/*
Edit by DateUpdated Function
=====================
Read Through File Stream
Insert Search String
String Comparison between Search String and Record DateUdated
Retrieve Data for String Comparison is True
Print Out Data
Flag For Edit The Printed Data
    if true Clear the Record by Goto change()
    if false Return Back Edit Menu Function
*/
void e_dateupdated()
{
    int a;
	int ch;
	FILE *fp;
	char check[10];
	int flag;
	system("cls");

	fp=fopen(fname,"rb+");
	
	printf("Please Enter Search String: \n");
	scanf("%s", check);

		while ( fread(&record, sizeof(record), 1, fp) && strcmp(check,record.date_updated) );

			ch=strcmp(check,record.date_updated);
				if(ch!=0)
				{
					printf("==RECORD NOT FOUND==\n");
					system("pause");
				}
				else
				{
                    system("cls");
					printf("==SEARCH SUCCESSFUL==\n");
					system("pause");
					system("cls");
				    printf("%s%9s%10s%20s\n","Name","Email","Phonenumber","DateUpdated");
                    for ( a = 0; a<=78; a++)
                    printf("=");
                    printf("\n");
					printf("%5.5s%9.8s%10.9s%20.19s\n", record.name, record.email, record.phonenumber, record.date_updated);
					printf("Edit this Record [1]/[0]: ");
					scanf("%1d", &flag);
					if (flag == 1)
                    {
                        change(fp,check, record.date_updated);
					    printf("RECORD Edited !!!\n");
					    system("pause");   
                    }
                    else
                    return;
				}
}

/*
Display Options
===============
Read through file stream
Display Options
*/
void display_options()
{
    
           system("cls"); 
           printf("Personal Address Book");
           printf("\n");
           printf("\n");
           printf("What you want to do\n");
           printf("\n");
           printf("[a]Search Record");
           printf("\n");
           printf("[b]Add Record");
           printf("\n");
           printf("[c]Delete Record");
           printf("\n");
           printf("[d]Edit Record");
           printf("\n");
           printf("[e]Display Options");
           printf("\n");
           printf("[f]Exit");
           printf("\n");
           printf("\n");
           
} 

/*
Clear Specific Data Memory
==========================
Create a Temporary Swap File Stream
Copy Specific Data Memory to Temporary Swap File Stream
Delete Original File Stream
Rename Temporary Swap File Stream to Original File Stream
*/
void empty(FILE *fp, char check[],char data[])
{
    FILE *temp;
    temp = fopen(fswap,"w");
    rewind(fp);
	while ( fread(&record, sizeof(record), 1, fp))
    {
        if (strcmp(check,data))
    	   fwrite(&record, sizeof(record), 1, temp);
    }
    fclose(fp);
    fclose(temp);
    remove(fname);
    rename(fswap, fname);
}


/*
Sort Specific Data Memory
==========================
Create a Temporary Swap File Stream
Sort Specific Data Memory to Temporary Swap File Stream
Delete Original File Stream
Rename Temporary Swap File Stream to Original File Stream
*/
void sort(FILE *fp, char data[], int type)
{
    FILE *temp;
    char a = 'A';
    char b = '0';
    temp = fopen(fswap,"w");
    rewind(fp);
    switch (type)
    {
        case 1:
        {
            
            while ( b >= '0' && b <= '9')
            {
                rewind(fp);
            	while ( fread(&record, sizeof(record), 1, fp))
                {
                    if (strncmp(data, &b ,1)== 0)
                	   fwrite(&record, sizeof(record), 1, temp);
                }
                b++;
            }
            
            while ( a >= 'A' && a <= 'z')
            {
                rewind(fp);
            	while ( fread(&record, sizeof(record), 1, fp))
                {
                    if (strncmp(data, &a ,1)== 0)
                	   fwrite(&record, sizeof(record), 1, temp);
                }
                a++;
            }
        break;
        }
        
        case 2:
        {
            char c[][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
            int i;
            for(i=0;i<=11;i++)
            {
                rewind(fp);
                while ( fread(&record, sizeof(record), 1, fp))
                {
                    if (strncmp(data, c[i] ,3)== 0)
                    	fwrite(&record, sizeof(record), 1, temp);
                }
            }
        break;
        }
            
    }
    
    fclose(fp);
    fclose(temp);
    remove(fname);
    rename(fswap, fname);
}

/*
Edit Specific Data Memory
=========================
Seeking The Address of Specific Data Memory
Enter Information for Edit Data Memory 
Edit Specific Data Memory by Overwrite the Original Specific Data Memory
*/
void change(FILE *fp, char check[],char data[])
{
    rewind(fp);
    while ( fread(&record, sizeof(record), 1, fp) && strcmp(check,data));
        if (!strcmp(check,data))
        {
            printf("Enter Name: ");
            scanf("%s", record.name);
            printf("Enter Email: ");
            scanf("%s", record.email);
            printf("Enter PhoneNumber: ");
            scanf("%s", record.phonenumber);
            printf("Enter DateUpdated: ");
            scanf("%s", record.date_updated);
            
            fseek(fp, ftell(fp) - sizeof(record),0);
			 fwrite(&record, sizeof(record), 1, fp);
        }
        fclose(fp);
}

void credit()
{
    puts("ECP1016 Take Home Assignment");
    puts("Topic 1: Simple Personal Address Book");
    puts("================================================");
    puts("");
    puts("Inquiry Lecturer: Mr. Chin");
    puts("");
    puts("Group Member");
    puts("------------");
    puts("1. Sia Saoh Liang  ID:1061103072  Lecture Group:EC104  Tutorial Group:ECD03");
    system("pause");
}

Amazing! I see you in a completely different light now. It's incredible how showing your code changes you from a leech into someone who deserves help.

>But some function does not working.
Which ones? No offense, but that's a lot of code to troubleshoot with only the "not working" bug report.

>i dun noe how to make the date and time automatically updated
I'm not sure if you're allowed to use it, but strftime is the easiest way to format a date and time. Just call it when you need to update the string and you're solid. Here's an example of how to use it for the current time:

#include <ctime>
#include <iostream>

int main()
{
  char dummy;

  do {
    char buffer[1024];
    time_t now = time ( 0 );

    strftime ( buffer, 1024, "%d-%m-%Y %H:%M:%S", localtime ( &now ) );
    std::cout<< buffer <<'\n';
  } while ( std::cin.get ( dummy ) );
}

Mr.Programmer,did you open the file i attached?Can u help me write the program or modified my code?

>did you open the file i attached?
Seeing as how that was the only way I could have known the required format for the date-time, it's safe to assume that I did. Thanks for asking though.

>Can u help me write the program or modified my code?
Can you read? Because I'm certain I asked you for more detail. If you fail to provide that detail, I'm not going to spend my valuable time rooting through your mass of code trying to find bugs when I have no vested interest in the program.

Mr.Programmer,i hope u will help me because i really don't know how to continue my work on it

How to add and search :if i add
jimmyneutron
neutron jimmy
how to make this two contact apppear when i type jimmy in search function?


And how to use the auto update time and date in my program?how to declare it?

It sounds like you need a map, or an algorithm to determine a match for a set amount of values.

Personally, to get things working, I'd take the easy way out until I found a more efficient solution.

What immediately comes to mind is something like this--

#include <iostream>
#include <map>
#include <utility>
#include <string>
#include <vector>

using std::string;
using std::map;
using std::pair;
using std::vector;

template<class L, class N> void safeInsert(map<L, vector<N> >&, L, N);
template<class L, class N> pair<L, vector<N> > safePair(L, vector<N>);

template<class L, class R>
void safeInsert(map<L, vector<R> > &m, L key, R value){
    if(&m[key] == NULL){
        vector<R> temp;
        temp.push_back(value);
        m.insert(safePair<L, R>(key, temp));
    }
    else m[key].push_back(value);
}

template<class L, class R>
pair<L, vector<R> > safePair(L lhs, vector<R> rhs){
    pair<L, vector<R> > p (lhs, rhs);
    return p;
}

int main(){

    map<string, vector<string> > myList;
    safeInsert(myList, string("Joe"), string("Joe"));
    safeInsert(myList, string("Joe"), string("Joe Smith"));
    safeInsert(myList, string("Bob"), string("Bob Marley"));

    vector<string> result = myList[string("Joe")];

    for(vector<string>::iterator next = result.begin(); next != result.end(); next++)
        std::cout << *next << std::endl;

    return 0;
}

-- though again it might not be perfect, but it meets the criteria without dipping far into algorithms or relying on queries to achieve similar results.

Edit: Rereading the post, I realized you need either a regex or a parser to determine the key to use the above method.

This link might also help - substr

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.