Hello I'm newbie in C programming. Iwant to make a simple contact book that have add, delete, edit, and display function. 2 out of 4 i've done which is add and delete. So I need some help from all of you in edit and list function. Here is my function for add and delete. I refer this from acidburn code and changed it a little bit and i don't want to use ctype.h because not familiar with it yet.
void Add()
{
char opt1;
do
{
Contactbook = fopen("Contactbook.txt","a+");
printf("Enter his/her Name:\n");
scanf("%s", contactbook.name);
printf("Enter his/her Birthday:\n");
scanf("%s", contactbook.birthday);
printf("Enter his/her Handphone No:\n");
scanf("%s", contactbook.hp);
printf("Enter his/her Address:\n");
scanf("%s", contactbook.address);
printf("Enter his/her Occupation\n");
scanf("%s", contactbook.occupation);
fprintf(Contactbook, "%s %s %s %s %s\n", contactbook.name,
contactbook.birthday, contactbook.hp, contactbook.address,
contactbook.occupation);
fclose(Contactbook);
printf("To Continue using this program type Y/y");
scanf("%c", &opt1);
}
}
void Del()
{
char opt2;
char Tar [SIZE];
int match = 0;
temp = fopen("temp.txt", "w");
if((Contactbook = fopen("Contactbook.txt","r")) == NULL)
printf("----The file is empty----\n");
else
printf("Enter his/her name:");
gets(Tar);
while (!feof(Contactbook))
{
fscanf(Contactbook,"%s %s %s %s %s",
contactbook.name, contactbook.birthday, contactbook.hp,
contactbook.address, contactbook.occupation);
if(feof(Contactbook))
break;
if(strcmp(Tar, contactbook.name) !=0)
fprintf(temp,"%s %s %s %s %s\n", contactbook.name,
contactbook.birthday, contactbook.hp, contactbook.address,
contactbook.occupation);
else
{
match = 1;
}
if (!match)
{
printf("The name does not exist\n");
printf("or it might had been deleted\n");
}
fclose(Contactbook);
fclose(temp);
remove("Contactbook.txt");
rename("temp.txt","Contacbook.txt");
}
So i need some help on edit and list function. At 1st i think i want to use the same code as delete but change remove to rename but it can't be done that's way. So your helps are highly appreciated. Ok. Thanks
You forgot to put [/code] at the end of code
Sorry 4 da mistake. Anyway, anybody can give me some ideas please.......
Sorry 4 da mistake. Anyway, anybody can give me some ideas please.......
I'd say first brush up on user input.
scanf("%s", contactbook.name);
gets(Tar);
User Input: Strings and Numbers [C]
while (!feof(Contactbook))
Avoid Loop Control Using eof()
And it might be helpful to post a program that attempts to use/test these functions rather than leaving that up to others. Presumeably you're writing test code that uses them? Share your "secret".
Use good formatting. Use correct spelling, punctuation, and grammar. Use code tags correctly. In short, make your post as easy to answer as you can -- or else get used to longer waits.
Thanks Dave for your advice. Sorry 4 not posting my whole code. Here's my full code. When run it, it didn't seems to run well.
I will seriously have a look your points and try to fix it. Anyway I'm still newbie, there's a lot of things to be learn. Your guidance will highly be appreciated. Actually this thing is my homework my teacher gave it to me and my friend, kind of group work. But I stucked at edit function. Thank You.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NAME 500
#define BIRTHDAY 500
#define HP 500
#define ADDRESS 500
#define OCCUPATION 500
#define SIZE 500
void Add();
void Del();
void Search();
void Edit();
void List();
void Exit();
char info[SIZE];
struct contactbook
{
char name [NAME];
char birthday [BIRTHDAY];
char hp [HP];
char address [ADDRESS];
char occupation [OCCUPATION];
char Tar [NAME];
}
contactbook;
FILE *Contactbook;
FILE *temp;
int main()
{
int a;
printf("Welcome to BRONTOK X Simple Contact Book\n");
printf("Please Choose your option\n");
printf("1: Adding The Contact\n");
printf("2: Delete The Contact\n");
printf("3: Search The Contact\n");
printf("4: Edit The Contact\n");
printf("5: List The Contact\n");
printf("6: Exit this Program\n");
scanf("%d", &a);
switch(a)
{
case 1:
{
Add();
break;
}
case 2:
{
Del();
break;
}
case 3:
{
Search();
break;
}
case 4:
{
Edit();
break;
}
case 5:
{
List();
break;
}
case 6:
{
Exit();
break;
}
default:
{
printf("Invalid Input");
}
}
}
void Add()
{
char opt1;
do
{
Contactbook = fopen("Contactbook.txt","a+");
printf("Enter his/her Name:\n");
scanf("%s", contactbook.name);
printf("Enter his/her Birthday:\n");
scanf("%s", contactbook.birthday);
printf("Enter his/her Handphone No:\n");
scanf("%s", contactbook.hp);
printf("Enter his/her Address:\n");
scanf("%s", contactbook.address);
printf("Enter his/her Occupation\n");
scanf("%s", contactbook.occupation);
fprintf(Contactbook, "%s %s %s %s %s\n", contactbook.name,
contactbook.birthday, contactbook.hp, contactbook.address,
contactbook.occupation);
fclose(Contactbook);
printf("To Continue using this program type Y/y");
scanf("%c", &opt1);
}
if(opt1 =='y' || opt1 == 'Y')
main();
}
void Del()
{
char opt2;
char Tar [SIZE];
int match = 0;
temp = fopen("temp.txt", "w");
if((Contactbook = fopen("Contactbook.txt","r")) == NULL)
printf("----The file is empty----\n");
else
printf("Enter his/her name:");
gets(Tar);
while (!feof(Contactbook))
{
fscanf(Contactbook,"%s %s %s %s %s",
contactbook.name, contactbook.birthday, contactbook.hp,
contactbook.address, contactbook.occupation);
if(feof(Contactbook))
break;
if(strcmp(Tar, contactbook.name) !=0)
fprintf(temp,"%s %s %s %s %s\n", contactbook.name,
contactbook.birthday, contactbook.hp, contactbook.address,
contactbook.occupation);
else
{
match = 1;
}
if (!match)
{
printf("The name does not exist\n");
printf("or it might had been deleted\n");
}
fclose(Contactbook);
fclose(temp);
remove("Contactbook.txt");
rename("temp.txt","Contacbook.txt");
do
{
printf("To Continue using this program type Y/y");
scanf("%c", &opt2);
}
while (opt2 =='y' || opt2 == 'Y');
main();
}
}
void Search()
{
char opt3;
char Tar [SIZE];
int match = 0;
if((Contactbook = fopen("Contactbook.txt","r")) == NULL)
printf("----The File is Empty----\n");
else
{
printf("Enter his/her name:\n");
scanf("%s", Tar);
while(!feof(Contactbook) && match ==0)
{
fscanf(Contactbook,"%s %s %s %s
%s",contactbook.name, contactbook.birthday, contactbook.hp,
contactbook.address, contactbook.occupation);
if(strcmp(Tar, contactbook.name) == 0)
match = 1;
}
if(match)
{
printf("His/Her Name is %s\n",contactbook.name);
printf("His/Her Birthday is s\n",
contactbook.birthday);
printf("His/Her Handphone No is %s\n",
contactbook.hp);
printf("His/Her Address is %s\n",
contactbook.address);
printf("His/Her Occupation is %s\n",
contactbook.occupation);
main();
}
else if (!match)
printf("----There is no such entry----");
fclose(Contactbook);
}
do
{
printf("To Continue using this program type Y/y");
scanf("%c", &opt3);
}
while (opt3 =='y' || opt3 == 'Y');
main();
}
void Edit()
{
void List()
{
int opt5;
Contactbook = fopen("Contactbook.txt","a+");
do
{
fgets(info,SIZE,Contactbook);
printf("%s\n", info);
}
while(!feof(Contactbook));
fclose(Contactbook);
do
{
printf("Enter y or Y to run the program again\n");
scanf("%c", &opt5);
}
while (opt5 == 'y' ||opt5 == 'Y');
main();
}
void Exit()
{
printf("Thank You For Using my Contactbook. Have a Nice
Day");
}I would encourage you to do just one function at a time.
Since your first function is Add(), I would comment all the other functions bodies and just have a statement like printf("in Del()"); etc. until you get there. Within Add() I would note:
1) each keyword do needs a keyword while.
2) Never call main() from within your program. Use loops to accomplish what you want. In this case you should use a loop in main() to cycle throught the menu and a loop in Add() to allow sequential Add()s without going through the menu, if that is your intent.
Check your Edit function. Where it ends?
The Edit function is now become my major problem because i have no idea on how to make it.
I would encourage you to do just one function at a time. Since your first function is Add(), I would comment all the other functions bodies and just have a statement like printf("in Del()"); etc. until you get there. Within Add() I would note:
OIC. I will change it to loop. But for the above statement could u please explain more. With examples if u don't mind
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 500
void Add();
void Del();
//void Search();
//void Edit();
//void List();
//void Exit();
char info[SIZE];
typedef struct contactboo
{
char name [SIZE];
char birthday [SIZE];
}contactbook;
FILE *Contactbook;
FILE *temp;
int main()
{
int a;
char flag = 'y';
while(flag != 'n')
{
printf("Welcome to BRONTOK X Simple Contact Book\n");
printf("Please Choose your option\n");
printf("1: Adding The Contact\n");
printf("2: Delete The Contact\n");
printf("6: Exit this Program\n");
scanf("%d", &a);
switch(a)
{
case 1:
Add();
break;
case 2:
Del();
break;
/*
case 3:
{
Search();
break;
}
case 4:
{
Edit();
break;
}
case 5:
{
List();
break;
}
*/
case 6:
flag = 'n';
break;
default:
printf("Invalid Input");
}
printf("Good bye!");
getch();
return 0;
}
void Add()
{
char opt1;
do
{
Contactbook = fopen("Contactbook.txt","a+");
printf("Enter his/her Name:\n");
scanf("%s", contactbook.name);
printf("Enter his/her Birthday:\n");
scanf("%s", contactbook.birthday);
fprintf(Contactbook, "%s %s\n", contactbook.name,
contactbook.birthday);
fclose(Contactbook);
printf("To Continue using this program type Y/y");
scanf("%c", &opt1);
}while(opt1 =='y' || opt1 == 'Y')
}
void Del()
{
printf("in Del()");
getch();
/*
char opt2;
char Tar [SIZE];
int match = 0;
temp = fopen("temp.txt", "w");
if((Contactbook = fopen("Contactbook.txt","r")) == NULL)
printf("----The file is empty----\n");
else
printf("Enter his/her name:");
gets(Tar);
while (!feof(Contactbook))
{
fscanf(Contactbook,"%s %s %s %s %s",
contactbook.name, contactbook.birthday);
if(feof(Contactbook))
break;
if(strcmp(Tar, contactbook.name) !=0)
fprintf(temp,"%s %s %s %s %s\n", contactbook.name,
contactbook.birthday, contactbook.hp, contactbook.address,
contactbook.occupation);
else
{
match = 1;
}
if (!match)
{
printf("The name does not exist\n");
printf("or it might had been deleted\n");
}
fclose(Contactbook);
fclose(temp);
remove("Contactbook.txt");
rename("temp.txt","Contacbook.txt");
do
{
printf("To Continue using this program type Y/y");
scanf("%c", &opt2);
}
while (opt2 =='y' || opt2 == 'Y');
main();
}
*/
}
/*
void Search()
{
char opt3;
char Tar [SIZE];
int match = 0;
if((Contactbook = fopen("Contactbook.txt","r")) == NULL)
printf("----The File is Empty----\n");
else
{
printf("Enter his/her name:\n");
scanf("%s", Tar);
while(!feof(Contactbook) && match ==0)
{
fscanf(Contactbook,"%s %s %s %s
%s",contactbook.name, contactbook.birthday, contactbook.hp,
contactbook.address, contactbook.occupation);
if(strcmp(Tar, contactbook.name) == 0)
match = 1;
}
if(match)
{
printf("His/Her Name is %s\n",contactbook.name);
printf("His/Her Birthday is s\n",
contactbook.birthday);
printf("His/Her Handphone No is %s\n",
contactbook.hp);
printf("His/Her Address is %s\n",
contactbook.address);
printf("His/Her Occupation is %s\n",
contactbook.occupation);
main();
}
else if (!match)
printf("----There is no such entry----");
fclose(Contactbook);
}
do
{
printf("To Continue using this program type Y/y");
scanf("%c", &opt3);
}
while (opt3 =='y' || opt3 == 'Y');
main();
}
void Edit()
{
void List()
{
int opt5;
Contactbook = fopen("Contactbook.txt","a+");
do
{
fgets(info,SIZE,Contactbook);
printf("%s\n", info);
}
while(!feof(Contactbook));
fclose(Contactbook);
do
{
printf("Enter y or Y to run the program again\n");
scanf("%c", &opt5);
}
while (opt5 == 'y' ||opt5 == 'Y');
main();
}
void Exit()
{
printf("Thank You For Using my Contactbook. Have a Nice
Day");
}
*/It would really help readability if you'd indent more. 1 space is hard to follow -- it's too much like none. Generally the recommendation is 3-4 spaces. Not a hard and fast rule, but a general rule. Readability is the key.
I've made a new coding here. But it seems to be a little error.
The two questions seems to come out together.
the output
Enter his/her name: <---- The problem
Enter his/her birthday:<--- is here
Enter his/her handphone:
Enter his/her Address:
Enter his/her Occupation:
<code>#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NAMES 80
#define BIRTHDAY 30
#define HANDPHONE 15
#define ADDRESS 120
#define OCCUPATION 50
#define SIZE 200
void Add();
void Del();
void Search();
void Edit();
void List();
void Exit();
char info[SIZE];
struct contactbook
{
char names [NAMES];
char birthday [BIRTHDAY];
char hp [HANDPHONE];
char address [ADDRESS];
char occupation [OCCUPATION];
char Target [SIZE];
}
contactbook;
FILE *Contactbook;
FILE *temp;
int main()
{
int option;
system("clear");
printf("\n WELCOME TO BRONTOK-X SIMPLE CONTACT BOOK\n");
printf("\n This program enables you to:\n");
printf("\n ADD, DELETE, EDIT, SEARCH, and LIST\n");
printf("\n your contact info =D\n\n");
printf("\n To start running this program, you have to choose an option from 1 to 6\n");
printf("\n 1: Adding Contact Info\n");
printf(" 2: Delete Your Contact Info\n");
printf(" 3: Search Your Contact Info\n");
printf(" 4: Edit Your Contact Info\n");
printf(" 5: List Down All Your Contact Info\n");
printf(" 6: Exit This Program\n\n");
printf(" Please Enter Your Choice: ");
scanf("%d", &option);
switch(option)
{
case 1:
{
system("clear");
Add();
break;
}
case 2:
{
system("clear");
Del();
break;
}
case 3:
{
system("clear");
Search();
break;
}
case 4:
{
system("clear");
Edit();
break;
}
case 5:
{
system("clear");
List();
break;
}
case 6:
{
system("clear");
Exit();
break;
}
default:
{
system("clear");
printf("Invalid Input");
break;
}
}
}
void Add()
{
Contactbook = fopen("Contactbook.txt","a+");
printf("Enter his/her Name:\n");
fflush(stdin);
fgets(contactbook.names, sizeof contactbook.names, stdin);
printf("Enter his/her Birthday:\n");
fflush(stdin);
fgets(contactbook.birthday, sizeof contactbook.birthday, stdin);
printf("Enter his/her Handphone No:\n");
fflush(stdin);
fgets(contactbook.hp, sizeof contactbook.hp, stdin);
printf("Enter his/her Address:\n");
fflush(stdin);
fgets(contactbook.address, sizeof contactbook.address, stdin);
printf("Enter his/her Occupation\n");
fflush(stdin);
fgets(contactbook.occupation, sizeof contactbook.occupation, stdin);
fprintf(Contactbook, "%s %s %s %s %s\n", contactbook.names,contactbook.birthday, contactbook.hp, contactbook.address, contactbook.occupation);
fflush(stdin);
fclose(Contactbook);
main();
}
void Del()
{
main();
}
void Search()
{
main();
}
void Edit()
{
main();
}
void List()
{
main();
}
void Exit()
{
system("clear");
}</code>Take another look at the "user input" link I posted.
Don't use fflush(stdin) .
Don't call main recursively.
Take another look at the "user input" link I posted. Don't usefflush(stdin). Don't callmainrecursively.
I've take another look at the "user input" but still didn't get what you mean. Removing fflush(stdin) will still give me the same result.
The weird thing is, this code worked nicely in windows but using knoppix it seems to be a little bit screw with the add() function, imean as i said previously the tqo question seems don't want to be separated. That's the only problem i have for now.One more thing, if i can't call main() recursively, can u suggest me any other way to call the main just after finish other function.
> One more thing, if i can't call main() recursively, can u suggest me any other way to call the main just after finish other function.
You don't need to call main; when your function finishes, it returns the calling function. If you need the code to repeat, use a loop within main.
>The weird thing is, this code worked nicely in windows but using knoppix it seems to be a little bit screw with the add()
Because what you were doing was undefined. It might work, or it might blow up your computer. Maybe this will help.
> One more thing, if i can't call main() recursively, can u suggest me any other way to call the main just after finish other function. You don't need to call main; when your function finishes, it returns the calling function. If you need the code to repeat, use a loop within main. >The weird thing is, this code worked nicely in windows but using knoppix it seems to be a little bit screw with the add() Because what you were doing was undefined. It might work, or it might blow up your computer. Maybe this will help.
hmmm........can u pls explain it in details bcoz i'm still newbie in C programming. and this is group work, so have to do some research on the topic. I still can't imagine how to use loop to recall a function. examples plz if u don't mind.
hmmm........can u pls explain it in details bcoz i'm still newbie in C programming. and this is group work, so have to do some research on the topic. I still can't imagine how to use loop to recall a function. examples plz if u don't mind.
Lerner already showed you here , about 6 posts ago.
Also, don't PM members to personally solve your problem for you. Ask questions here, read what they say, and attempt what they tell you.
Lerner already showed you here , about 6 posts ago.
Also, don't PM members to personally solve your problem for you. Ask questions here, read what they say, and attempt what they tell you.
i already tried the code that was given by Lerner, but it seems to be have many error that i can't figure out . "parse error before "contactbook" " and other two more error which i don't remember. And i don't used and don't understand getch() function.
Sorry for my bad manner in using PM.
i already tried the code that was given by Lerner, but it seems to be have many error that i can't figure out . "parse error before "contactbook" " and other two more error which i don't remember. And i don't used and don't understand getch() function.
You don't need to compile and run his code. Look at what he did in main() to set up a loop. Then you can stop calling main() from your functions.
in the previous post Dave had stated not to use infinite loop !feof but the example given is in C++, is there any example in C ? what should i replace !feof with?