hi guys,Iam learning the c language and still a green one really.I have this program am trying to develop using code blocks 10.5.Th code is as follows;

#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>


  struct secguard
{
    char first_name[40];
    char Last_name[40];
    char gender;
    int age;
    int ID;
    char start_date;
    int work_rec;
};

void main(void)
{
    struct secguard guard_rec[300];
    FILE*GUARD_DETAILS;
    char operation;
    char gender;
    if((GUARD_DETAILS=fopen("security.dat","wb+"))== NULL)
    {
        printf("File can't open!");
    }
    else
    {

    switch(operation)
    {
        printf("Please choose the operation you want to perform.\n");
        scanf("%c",operation);
        {

        case 'A' :printf("Add record(s) to the database.\n");
        break;
        case 'U' :printf("Update record(s).\n");
        break;
        case 'Q' :printf("Query database to obtain information.\n");
        break;
        default :printf("Undefined operation!");
        break;

    }

    if(operation=='A'||operation=='a')
    {
        printf("Please,enter surname.\n");
        fflush(stdin);
        gets(guard_rec.first_name);
        printf("Please,enter lastname.\n");
        fflush(stdin);
        gets(guard_rec.Last_name);
        printf("Please,enter gender.\n");
        scanf("%c",&gender);
    }
    else
    {
        printf("Sorry you have no more choices!");
    }

    }
    }
    fclose(GUARD_DETAILS);
}

when I compiled this code,it generated errors both being of the same nature.it read that first_name is a member of something that isn't a structure or union.Now,where could I have gone wrong?This is just a small extract.

guard_rec is an array, not a struct instance. You need to index into the array before any member access can be done:

/* Where i is a suitable index */
guard_rec[i].first_name

Your created an array of structures

struct secguard guard_rec[300];

So to access one element of your array you must use an array index like

gets(guard_rec[0].first_name);

Oh by the way the use of gets is really frowned upon...really. Here's why

BUGS
Never use gets(). Because it is impossible to tell without knowing the
data in advance how many characters gets() will read, and because
gets() will continue to store characters past the end of the buffer, it
is extremely dangerous to use. It has been used to break computer
security. Use fgets() instead.

Ooops Narue got here first

Hi guys,thanks for the remedies.However on using fgets the compiler generated two errors of the same nature.It was like 'too few arguements passed to function 'fgets''.I am trying to read more about fgets and I will see what i'll come up with.For now thanks!

Hi guys,thanks for the remedies.However on using fgets the compiler generated two errors of the same nature.It was like 'too few arguements passed to function 'fgets''.I am trying to read more about fgets and I will see what i'll come up with.For now thanks!

Take this in the way its intended...or not. Did you actually google how to use fgets()?

hi guys,i thank you very much for the time you spend for guys like me who are new to the language.while searching for the use of fgets,i came across this individual who also happened to use this platform.he wanted to be advised on how to use fgets() and he was advised to use getchar instead and his response was positive.Here is the code:

1. // Data Structure
2. typedef struct{
3. char first_name[20];
4. char middle_name[20];
5. char last_name[20];
6. char illness_type[5];
7. int patient_number;
8. char doctor_fname[20]; // Doctor's First Name
9. char doctor_lname[20]; // Doctor's Last Name
10. char emer_fname[20]; // Emergency Contact's First Name
11. char emer_lname[20]; // Emergency Contact's Last Name
12. int emer_telephone; // Emergency Contact's Number 13.} PATIENT_DATA;
14.
15.// Defining data type
16.PATIENT_DATA section_1[40]= {"","","","",0,"","","","",0}; 17. 18.//METHOD 1
19.printf("\nPatient First Name: ");
20.fflush(stdin
21.fgets(section_1[tally1].first_name, sizeof section_1[tally1].first_name, stdin);
22.
23.//METHOD 2
24.printf("\nPatient First Name: ");
25.fflush(stdin);
26.fgets(section_1[tally1].first_name, sizeof section_1[20].first_name, stdin);


I just want to learn something from this code.Why did this fella bring in line 16?Can you guys comment on my code because i tried to compile it and it crashed.Anybody that can educate me about fgets is welcome for my code i used gets which didn't generate any errors like fgets.
mixmags,thanks for the link.

This code has some things that you should avoid like

fflush(stdin);

This is a no no.

Plus this

PATIENT_DATA section_1[40]= {"","","","",0,"","","","",0};

I'm not sure what the effect is here...Does it initialize the first element? I'm not really sure, one of the C language lawyers will have to comment on this one..

Here's a cleaned up version of the posted code

#include <stdio.h>

typedef struct
{
	char first_name[20];
	char middle_name[20];
	char last_name[20];
	char illness_type[5];
	int patient_number;
	char doctor_fname[20];
	char doctor_lname[20];
	char emer_fname[20];
	char emer_lname[20];
	int emer_telephone;
} PATIENT_DATA;

int main()
{ 

	PATIENT_DATA section_1 = {"","","","",0,"","","","",0};
	printf("\nPatient First Name: ");

	fgets(section_1.first_name, sizeof(section_1.first_name), stdin);

	return 0;
}

Please note, if you post code use the code tags.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.