thanks for the help all, i had to do a phonebook program and it just worked me.
basically the main.c file is for the menu and call functions.
the main.h file holds the struct.
the process.c file has the actual functions.
process.h holds the prototypes for those functions.
this program is a phonebook that allows the user to add entries, edit entries, and print the file. thanks for the help, i need it.
main.c
Code:
#include <stdio.h>
#include <stdlib.h>
#include "main.h"
int find(c*, int);
int main() {
int entry;
c * newc;
int choice;
int counter = 0;
while (choice != 4) {
printf("Welcome to the Phonebook\n\n");
printf("1)Add entry.\n");
printf("2)To edit entry.\n");
printf("3)Print Phonebook.\n");
printf("4)Quit.\n");
scanf("%d", &choice);
if (choice == 1){
if(counter == 0) {
newc = (c*) calloc(1,sizeof(c)); }
else
newc = (c*) realloc(newc, (counter+1) * sizeof(c));
add (newc, counter);
counter++;
}
if (choice == 2) {
int i;
char firstNameSearch[20], lastNameSearch[20];
printf("Enter the first name: ");
scanf("%s", firstNameSearch);
printf("Enter the last name: ");
scanf("%s", lastNameSearch);
for( i = 0; i < counter; i++)
{
if( strcmp(firstNameSearch,newc[i].first) && strcmp(lastNameSearch,newc[i].last) )
return i;
else {
printf("That person is not a contact.\n");
}
edit(newc,entry);
}
}
if (choice == 3)
{
write(newc, counter);
printf("Phonebook written to file.\n");
}
}
system("PAUSE");
return 0;
}
main.h
Code:
typedef struct contact {
char first[20];
char last[20];
char email[20];
char phone[12];
} c;
process.h
Code:
#include "main.h"
void add( c*, int );
void edit( c*, int );
void write_file( c*, int );
process.c
Code:
#include "main.h"
void add(c* contacts, int entry) {
printf("Enter first name: ");
scanf("%s", contacts[entry].first);
printf("Enter last name: ");
scanf("%s", contacts[entry].last);
printf("Enter email: ");
scanf("%s", contacts[entry].email);
printf("Enter phone: ");
scanf("%s", contacts[entry].phone);
}
void edit(c* contacts, int entry) {
printf("Please enter the new email: ");
scanf("%s",contacts[entry].email);
printf("Please enter the new phone number: ");
scanf("%s",contacts[entry].phone);
printf("Thank you for editing the info.");
}
void write(c* contacts, int num) {
FILE * out_file;
out_file = fopen( "H:\\school\\myprogram\\phonebook.txt", "w" );
int i;
for (i = 0; i < num; i++){
fprintff (out_file,"%s \t%s \t%s \t%s\n",contacts[i].first, contacts[i].last, contacts[i].email, contacts[i].phone);
}
fclose(out_file);
}