User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 402,884 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,034 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 6192 | Replies: 50
Closed Thread
Join Date: May 2006
Posts: 2,700
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 14
Solved Threads: 219
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: C programming - need some help

  #11  
Aug 22nd, 2006
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.
 
Join Date: Aug 2006
Posts: 24
Reputation: amano is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
amano amano is offline Offline
Newbie Poster

Re: C programming - need some help

  #12  
Aug 29th, 2006
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:


#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");
}
 
 
Join Date: Apr 2004
Posts: 3,473
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 141
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: C programming - need some help

  #13  
Aug 29th, 2006
Take another look at the "user input" link I posted.
Don't use fflush(stdin).
Don't call main recursively.
 
Join Date: Aug 2006
Posts: 24
Reputation: amano is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
amano amano is offline Offline
Newbie Poster

Re: C programming - need some help

  #14  
Aug 29th, 2006
Originally Posted by Dave Sinkula View Post
Take another look at the "user input" link I posted.
Don't use fflush(stdin).
Don't call main recursively.


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.
 
Join Date: Jul 2006
Posts: 65
Reputation: GloriousEremite will become famous soon enough GloriousEremite will become famous soon enough 
Rep Power: 4
Solved Threads: 14
GloriousEremite GloriousEremite is offline Offline
Junior Poster in Training

Re: C programming - need some help

  #15  
Aug 29th, 2006
> 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.
Last edited by GloriousEremite : Aug 29th, 2006 at 11:27 pm.
"What are the roots that clutch, what branches grow
out of this stony rubbish?"
 
Join Date: Aug 2006
Posts: 24
Reputation: amano is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
amano amano is offline Offline
Newbie Poster

Re: C programming - need some help

  #16  
Aug 30th, 2006
Originally Posted by GloriousEremite View Post
> 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.
 
Join Date: May 2006
Posts: 2,700
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 14
Solved Threads: 219
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: C programming - need some help

  #17  
Aug 30th, 2006
Originally Posted by amano View Post
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.
Last edited by WaltP : Aug 30th, 2006 at 1:14 am.
 
Join Date: Aug 2006
Posts: 24
Reputation: amano is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
amano amano is offline Offline
Newbie Poster

Re: C programming - need some help

  #18  
Aug 30th, 2006
Originally Posted by WaltP View Post
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.
 
Join Date: May 2006
Posts: 2,700
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 14
Solved Threads: 219
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: C programming - need some help

  #19  
Aug 30th, 2006
Originally Posted by amano View Post
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.
 
Join Date: Aug 2006
Posts: 24
Reputation: amano is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
amano amano is offline Offline
Newbie Poster

Re: C programming - need some help

  #20  
Aug 31st, 2006
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?
 
Closed Thread

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 2:30 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC