Reputation: 17
struct contact
{
char name[20];
char email[20];
int hpnum;
}add;
FILE *f;
void addcontact(struct contact list[3]);
void savecontact(struct contact list[3]);
int main (void)
{
int option,i;
struct contact list[3];
do
{
system("cls");
printf("==========Welcome to Jeffery's Contact System Management==========\n");
printf("\t\t\tContact System Main Menu\n");
printf("[1] Create a New Contact\n");
printf("[2] Modified Existing Contact\n");
printf("[3] Delete Existing Contact\n");
printf("[4] Search Existing Contact\n");
printf("[5] Exit\n");
printf("Please enter one of your option.\n");
scanf("%d",&option);
switch(option)
{
//add new contact
case 1:addcontact(list);savecontact(list);
break;
case 2:
case 3:
case 4:for(i=0;i<3;i++)
{
if(list[i].email!=0){
printf("\nContact Name: %s",list[i].name);
printf("\nHandphone Number: %d",list[i].hpnum);
printf("\nE-mail: %s",list[i].email);
}
}
getch();
break;
case 5:exit(EXIT_SUCCESS);
}
}while(i=1);
getch();
}
void addcontact(struct contact list[3])
{
char name[20],email[20];
int hpnum,no=0;
fflush(stdin);
printf("\nContact Name: ");
scanf("%s",list[no].name);
fflush(stdin);
printf("\nHandphone Number: ");
scanf("%d",&list[no].hpnum);
fflush(stdin);
printf("\nE-mail: ");
scanf("%s",list[no].email);
}
void savecontact(struct contact list[3])
{
FILE *f;
f=fopen("contact.txt","w");
fwrite(list,sizeof(list),3,f);
fclose(f);
}
After I added the contact, it does store it when I searching back inside the cmd. And it included the weird word inside the searching contact section when I search the contact. besides, the txt file that it execute, it's nothing inside that what i wrote. It just consist the weird word.
Upvotes: 0
Views: 172
Reputation: 58291
fflush(stdin);
is undefined error. should use fflush(stdout);
in savecontact()
function fwrite(list,sizeof(list),3,f);
is wrong. it should be
fwrite(list, sizeof(struct contact), 3, f);
Declaration even wrong void addcontact(struct contact list[3])
should be
void addcontact(struct contact *list)
EDIT
corrected your code. Please read comments I added in your code.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct contact
{
char name[20];
char email[20];
int hpnum;
}add;
FILE *f;
void addcontact(struct contact list[3]);
void savecontact(struct contact list[3]);
int main (void){
int option ;
struct contact list[1]; // chnages the size of Lint to One
do
{
printf("==========Welcome to Jeffery's Contact System Management==========\n");
printf("\t\t\tContact System Main Menu\n");
printf("[1] Create a New Contact\n");
printf("[2] Modified Existing Contact\n");
printf("[3] Delete Existing Contact\n");
printf("[4] Search Existing Contact\n");
printf("[5] Exit\n");
printf("Please enter one of your option.\n");
scanf("%d",&option);
switch(option)
{
//add new contact
case 1:
addcontact(list);
savecontact(list);
break;
case 2:
case 3:
case 4: // read from file instead from memroy (list)
f=fopen("contact.txt","rb");
while(fread(list,sizeof(struct contact),3,f)>0)
printf("%s %d %s\n",list[0].name, list[0].hpnum, list[0].email);
fclose(f);
break;
case 5:exit(EXIT_SUCCESS);
}
}while(1); // changed to 1 instead i=1 and remove i variable from declaration
}
void addcontact(struct contact *list) // Declaration corrected
{
int no=0;
fflush(stdout); // fflush(stdin); was wrong! (this line not needed here)
printf("\nContact Name: ");
scanf("%s",list[no].name);
fflush(stdout);
printf("\nHandphone Number: ");
scanf("%d",&list[no].hpnum);
fflush(stdout);
printf("\nE-mail: ");
scanf("%s",list[no].email);
}
void savecontact(struct contact *list) // Declaration corrected
{
// remove redeclaration of `File* f`
f=fopen("contact.txt","ab"); // open file in a+ mode instead w
fseek(f, 0, SEEK_END); // Added this line to shift to end of file
fwrite(list,sizeof(struct contact),3,f); //Corrected this line
fclose(f);
}
And its working, A run:
:~$ gcc c.c -Wall
:~$ ./a.out
==========Welcome to Jeffery's Contact System Management==========
Contact System Main Menu
[1] Create a New Contact
[2] Modified Existing Contact
[3] Delete Existing Contact
[4] Search Existing Contact
[5] Exit
Please enter one of your option.
1
Contact Name: grijesh
Handphone Number: 123
E-mail: g@123
==========Welcome to Jeffery's Contact System Management==========
Contact System Main Menu
[1] Create a New Contact
[2] Modified Existing Contact
[3] Delete Existing Contact
[4] Search Existing Contact
[5] Exit
Please enter one of your option.
1
Contact Name: yourname
Handphone Number: 1234
E-mail: your@email
==========Welcome to Jeffery's Contact System Management==========
Contact System Main Menu
[1] Create a New Contact
[2] Modified Existing Contact
[3] Delete Existing Contact
[4] Search Existing Contact
[5] Exit
Please enter one of your option.
4
grijesh 123 g@123
yourname 1234 your@email
Upvotes: 1
Reputation: 6565
Your are accepting a single contact
in addcontact
function and is saving 3 objects in savecontact
function.
Your addcontact
function should be like this
void addcontact(struct contact *list)
{
char name[20],email[20];
int hpnum,no=0;
fflush(stdout);
printf("\nContact Name: ");
scanf("%s",list->name);
fflush(stdout);
printf("\nHandphone Number: ");
scanf("%d",&list->hpnum);
fflush(stdout);
printf("\nE-mail: ");
scanf("%s",list->email);
}
call it like this
struct contact list;
addcontact(&list);
Upvotes: 2