Thread: HELP!!! cant figure this out

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    8

    Smile HELP!!! cant figure this out

    Hello everyone.

    I would like to ask for assistance is this program. When i compile it it gives me 5 errors, but i have tried my best and still not manage it to compile . Im not asking for it to be totally fixed, i would appreciate if someone took the time to look at it and tell me what i should do it fix it.

    the program is supposed to read record from mydata.txt , and print them out.

    mydata.txt

    Code:
    Bridget Veksler 324863311 miami FL 22 female
    Eric Jones  111223333 Austin Tx 53 male
    Anton Wang 563879290 FortLauderdale FL 60 male
    Shazmee Wass 888990000 Miami FL female 18
    Chas Wanio 123456789 BocaRaton FL male 33
    Lofton Bullard 112233449 BocaRaton FL male 30
    Thomas Fernandez 999999999 BocaRaton FL male 50
    Joy Woodworth 098765432 DelrayBeach FL female 21
    Tami Sargente 543987657 BoyntonBeach FL female 19
    Wayne Wass 888000000 Miami FL male 45
    Chris Wanio 12000789 BocaRaton FL male 38
    Mary Bullard 100003449 BocaRaton FL female 70
    Dina Fernandez 900000999 BocaRaton FL female 52
    John Woodworth 098700002 DelrayBeach FL male 41
    Tom Sargente 543000057 BoyntonBeach FL male 29

    COde

    Code:
    #include <stdio.h>
    
    #include <string.h>
    
    #define SIZE 30
    
    
    typedef struct
    
    {
    
    char firstname[30],lastname[30],ssno[30],city[30],state[30],year[30],sex[30];
    
    }student_record;
    
    /*Function Prototypes*/
    
    void initialize(student_record DB[30],int &count);
    
    int IsEmpty(int);                                       /*prototype for IsEmpty*/
    
    int IsFull(int) ;                                       /*prototype for isFull*/
    
    void search(student_record DB[30],int count) ;               /* prototype for search*/
    
    void print(student_record DB[30], int count) ;              /*prototype for print*/
    
    void insert(student_record DB[30],int *count) ;             /* for insert*/
    
    void remove (student_record DB[30],int *count) ;         /* prototype for remove*/
    
    /********************************************************
    
    
    *********************************************************/
    
     void initialize(student_record database[30],int &count)
    {
                FILE  *input;
    
        char temp_str[30];
    
        input = fopen("mydata.txt","r");  /*open file*/
    
        while(!feof(input) && count<SIZE)
    
        {
    
            fscanf(input,"%s",temp_str);
    
            strcpy(database[30].lastname,temp_str);
    
            fscanf(input,"%s",temp_str);
    
            strcpy(database[30].firstname,temp_str);
    
            fscanf(input,"%lf",temp_str);
    
            strcpy(database[30].ssno,temp_str);
    
            fscanf(input,"%s",temp_str);
    
            strcpy(database[30].city,temp_str);
    
            fscanf(input,"%s",temp_str);
    
            strcpy(database[30].state,temp_str);
    
            fscanf(input,"%lf",temp_str);
    
            strcpy(database[30].year,temp_str);
    
            fscanf(input,"%s",temp_str);
    
            strcpy(database[30].sex,temp_str);
    
    
    
             fclose(input);
        }
    
    
    }
    
    /********************************************************
    
    
    *********************************************************/
    
    
    int Is_Empty(int count)
    {
    	return count==0;
    }
    
    /********************************************************
    
    
    *********************************************************/
    
    int Is_Full(int count)
    {
    	return count==SIZE;
    }
    
    /********************************************************
    
    
    *********************************************************/
    
    
    int search(student_record DB[], int count, char key[])
    {
    int i;
    	for(i=0; i<count; i++)
    		if(strcpy(key,DB[i].firstname,DB[i].lastname)==0)
    			return i;
    	return -1;
    
    }
    
    
    /********************************************************
    
    
    
    *********************************************************/
    
    
    void print(student_record DB[], int count)
    {
    	int i=0;
    	while(i<count)
    	{
    		printf("%S %s %lf %s %s %lf %c \n",DB[i].lastname,DB[i].firstname,DB[i].ssno,DB[i].city,DB[i].state,DB[i].sex);
    		i++;
    	}
    
    
    /********************************************************
    
    
    *********************************************************/
    
    void remove_record(student_record DB[], int * count, char key[])
    
    {
    	int i = search(DB, *count, key);                    //***
    
    
    	if (i != -1)
    	{
    		((*count)--);
    		for(; i<*count; i++)
    			DB[i] = DB[i+1];
    	}
    }
    
    
    /********************************************************
    
    
    *********************************************************/
    
    void insert(student_record DB[], int * count)
    
    {	if(*count < SIZE)                               // ****
    
    	{
    		printf("Enter first name: ");
    		scanf("%s", &DB[*count].firstname);
    		printf("Enter last name: ");
    		scanf("%s", &DB[*count].lastname);
    		printf("Enter ssno: ");
    		scanf("%lf", DB[*count].ssno);
    		printf("Enter city: ");
    		scanf("%s", DB[*count].city);
    		printf("Enter state: ");
    		scanf("%s", DB[*count].state);
    		printf("Enter year: ");
    		scanf("%lf", DB[*count].year);
    		(*count)++;
    	}
    	else
    		printf("Cannot add item: database is full.");
    }
    
    
    int main()
    
    {
    student_record database[SIZE];                            //database for student records
    
    char command;                                             //holds the command to execute
    
    int count=0;                                               //contains count of actual number of items in database
    
    initialize(database,count);
    
    printf("Enter a command: s=search, p=print, r=remove,i=insert, e=exit\n");
    
    scanf(" %c", &command);
    
    
    
    while (command!='e')
    
    
    
    switch (command)
    
    
    
    case 's' :printf("Enter s if you want to search");
                search() ;
                break;
    
    case 'p' : printf("Enter p if you want to print");
                    print_array();
                    break;
    
    case 'r': printf("Enter r if you want to remove");
                remove_record ();
                break;
    
    case 'i': printf("Enter i if you want to insert");
                insert();
                break;
    
    
    
    printf("Enter acommand:s=search,p=print,r=remove,i=insert,e=exit\n");
    
    scanf(" &c", &command);
    
    
    
    return 0;
    }
    }
    any help would be appreciated
    THANK YOU!!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It would help if you posted what those errors are [including which line they are on].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    I got a lot of errors when I compiled your program. Can you be more specific on what errors are stumping you? The first error according to my compiler is on line 18 where your function prototype:
    Code:
    void initialize(student_record DB[30],int &count);
    This is incorrect. It should be int* count. Start with fixing the first error, then recompile, repeat.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    A few of the issues I see:

    #1. In your initialize function:
    Code:
    fscanf(input,"%s",temp_str);
    strcpy(database[30].lastname,temp_str);
            
    fscanf(input,"%s",temp_str);
    strcpy(database[30].firstname,temp_str);
    
    fscanf(input,"%lf",temp_str);
    strcpy(database[30].ssno,temp_str);
    
    fscanf(input,"%s",temp_str);
    strcpy(database[30].city,temp_str);
    
    fscanf(input,"%s",temp_str);
    strcpy(database[30].state,temp_str);
    
    fscanf(input,"%lf",temp_str);
    strcpy(database[30].year,temp_str);
    
    fscanf(input,"%s",temp_str);
    strcpy(database[30].sex,temp_str);
    All those indicies are out of bounds. Your array only contains 30 members and since arrays are zero based, index 30 is actually the 31st member which does not exist. I believe you want to somehow use the count variable that you passed into the function.



    #2. Also, in the initialize function:
    Code:
    FILE  *input;
    
    ...
    
    input = fopen("mydata.txt","r");  /*open file*/
    
    while(!feof(input) && count<SIZE)
    {
        ...
    
        fclose(input);
    }
    The position of that fclose means that the file will be closed after just the first loop. That should be after the loop ends. There may also be a problem with the way that you've constructed the loop condition. The EOF indicator for a file will only get set after an attempt to read has failed. If you've successfully read the last line of the file, this means that EOF is false according to this indicator and you would possibly then go through the loop one more time even though there is no more data to be read. A better way would be to read each line using fgets into a buffer and test the result of that in your while loop's condition. Then, within the loop you can use sscanf on that buffer to read the individual pieces of the line into your struct:
    Code:
    char buffer[220];
    
    ...
    
    while( fgets(buffer,sizeof(buffer),input)!=NULL && count<SIZE)
    {
        /* Use sscanf here on the buffer to parse the line you've read */
    }
    fclose(input);


    #3. In your search function:
    Code:
    int search(student_record DB[], int count, char key[])
    {
        int i;
        for(i=0; i<count; i++)
            if(strcpy(key,DB[i].firstname,DB[i].lastname)==0)
    	    return i;
        return -1;
    }
    Why are you trying to use strcpy here (and incorrectly at that)? I think you want to use strcmp instead perhaps (and neither of those take 3 arguments)?



    #4. In your print function:
    Code:
    void print(student_record DB[], int count)
    {
        int i=0;
        while(i<count)
        {
            printf("%S %s %lf %s %s %lf %c \n",DB[i].lastname,DB[i].firstname,DB[i].ssno,DB[i].city,DB[i].state,DB[i].sex);
            i++;
        }
    You are apparently missing an ending brace '}' to complete that function unless it is a typo of some sort. Your printf statement has 7 format specifiers for output, 3 (or 4?) of which are wrong given that all of your structure members are char[30] variables, but only 6 subsequent arguments are provided.



    #5. In your insert function:
    Code:
    {
        printf("Enter first name: ");
        scanf("%s", &DB[*count].firstname);
        printf("Enter last name: ");
        scanf("%s", &DB[*count].lastname);
        printf("Enter ssno: ");
        scanf("%lf", DB[*count].ssno);
        printf("Enter city: ");
        scanf("%s", DB[*count].city);
        printf("Enter state: ");
        scanf("%s", DB[*count].state);
        printf("Enter year: ");
        scanf("%lf", DB[*count].year);
        (*count)++;
    }
    The & are unnecessary since these members are already address's (strings/C-style char arrays). The %lf's are just plain wrong since again, these are char array's.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 3 dimensional figure volume
    By thekautz in forum C++ Programming
    Replies: 2
    Last Post: 01-20-2009, 05:22 PM
  2. Can't figure out why this program won't work
    By Northstar in forum C Programming
    Replies: 6
    Last Post: 11-26-2007, 11:31 AM
  3. trying to figure out someone's code for a plugin
    By paulpars in forum C++ Programming
    Replies: 4
    Last Post: 07-20-2006, 10:57 AM
  4. newb to C, cant figure something out.
    By Phate4219 in forum C Programming
    Replies: 16
    Last Post: 03-06-2006, 01:47 AM
  5. cant figure out where to put the loop....
    By seal in forum C Programming
    Replies: 2
    Last Post: 08-30-2005, 10:10 AM