0

Now I having another problem with that, after I changed my coding as shown below, it still showing out the error. Inside the coding, there was no any red underline, therefore I can't find out where's the error. So any error of this coding?

struct contact
{
    char name[20],email[20];
    int hpnum;
}add;
int option;
FILE *f;

void addcontact(struct contact list[100]);
void read(struct contact list[100]);

int main (void)
{
    struct contact list[100];
    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);read(list);
        break;
    }

    getch();
}

void addcontact(struct contact list[100])
{
    char name[20],email[20];
    int hpnum,no;

    printf("\nContact Name: ");
    scanf("%s",list[no-1].name);
    fflush(stdin);
    printf("\nHandphone Number: ");
    scanf("%d",&list[no-1].hpnum);
    fflush(stdin);
    printf("\nE-mail: ");
    scanf("%s",list[no-1].email);
}

void read(struct contact list[100])
{
  FILE *f;
  f=fopen("contact.txt","w");
  fwrite(list,sizeof(list),100,f);
  fclose(f);
}
John3136
  • 28,809
  • 4
  • 51
  • 69
  • 1
    Can you be more specific about what "it still showing out the error" means? – aschepler Feb 27 '13 at 20:57
  • When i running the problem, there will be no error, once I choose the 1st to add contact, there will be an error came out...the error is Run-Time Check Failure #3 - The variable 'no' is being used without being initialized. If there is a handler for this exception, the program may be safely continued. – Leo Jie Hui Feb 27 '13 at 21:06

1 Answers1

0

First, fflush(stdin); is undefined error. should use fflush(stdout);

Second, in function addcontact(struct contact list[100]) variable no has not assigned any value and you are using in scanf() function with garbage value.

Third, in read() function fwrite(list,sizeof(list),100,f); is wrong it should be like.

fwrite(list, sizeof(struct contact), 100, f);

I am not sure but it looks You are intended to read more then one contacts So you also need a looping mechanism probably inside addcontact() function

Community
  • 1
  • 1
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
  • I changed to stdout and i already initial the value of no equal to zero. I can finish my problem...now the problem is after i type all the information, the txt file does execute. But inside the txt file, it does not show any information. – Leo Jie Hui Feb 27 '13 at 21:10
  • If you initialize no to zero then `list[no-1]` become buggy because `no-1` would be `-1`. You need to give +ve value to no... – Grijesh Chauhan Feb 27 '13 at 21:17
  • Yes. There is no bug now. But now the problem is after the txt file executed, inside the txt file has no my contact information. – Leo Jie Hui Feb 27 '13 at 21:21
  • @LeoJieHui updated answer. Try again & in `addcontact()` write `list[no]` when `no` should be a valid +ve value. – Grijesh Chauhan Feb 27 '13 at 21:28
  • Erm...it's still the same happened in the txt file. The txt file doesn't consist the thing that what i typed in cmd. It just displaying the word that are weird. – Leo Jie Hui Feb 27 '13 at 21:37
  • @LeoJieHui Why do you trying with full data 100...first try with 3-4 list size. enter statically all record manually in list in code. then step-by-step improve your code. – Grijesh Chauhan Feb 27 '13 at 21:39
  • Hmm...then I try to change a bit of coding now and see. Anyway, thanks for you instructions. =) – Leo Jie Hui Feb 27 '13 at 21:42