A little Problem!

This is a discussion on A little Problem! within the C Programming forums, part of the General Programming Boards category; Would anybody be able to take a look and tell me why this code does not work as i have ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    6

    A little Problem!

    Would anybody be able to take a look and tell me why this code does not work as i have spent ages trying to correct 2 errors and cant find anything wrong with it. ( I am aware that there is not yet a complete if statement within the delete function but it should work as far as showing details and adding is concerned)


    [CODE]#include <stdio.h>
    #include <stdlib.h>
    #define FALSE 0
    #define TRUE 1
    #define NAME 20
    #define CODE 7

    typedef struct house{
    char surname[NAME];
    char postcode
    Code:
    ;
    		int rooms;
    		int price;
    	      }House;
    
    void read(House arrayofrecs[], int *noofhouses);
    void List(House arrayofrecs[], int *noofhouses);
    void addhouse(House arrayofrecs[], int *noofhouses);
    void deletehouse(House arrayofrecs[], int *noofhouses);
    
    
    void main ()
    {
       house list[20];
       int val;
       int noinlist;
    
       read(list, &noinlist);
    
       printf("\nOPTIONS\n");
       printf("1.Print the list of houses\n");
       printf("2.Add a new house\n");
       printf("3.Remove a house from list as specified by owner name\n");
       printf("4.Output records to file\n");
       printf("5.Calculate the average price of the houses\n");
       printf("6.Output houses with the requested no.of rooms\n");
       printf("Choose an option between 1 and 6\n");
       scanf("%d", &val);
       if (val==1)
          List(list, &noinlist);
       else if (val==2)
          addhouse(list, &noinlist);
       else if (val==3)
          deletehouse(list, &noinlist);
    
    
    void read(House arrayofrecs[], int *noofhouses) {
       int i;
       FILE *fp;
       fp=fopen("a:\\House.txt","r");
       printf("No on list?\n");
       fscanf(fp, "%d", noofhouses);
       i=0;
       while(i<*noofhouses)
       {
          fscanf(fp, "%s %s %d %d", &arrayofrecs[i].surname, &arrayofrecs[i].postcode, &arrayofrecs[i].rooms, &arrayofrecs[i].price);
          i++;
       }
       fclose(fp);
    
    }
    
    void List(House arrayofrecs[], int *noofhouses) {
       int i;
       printf("List of houses \n");
       i=0;
       while(i < noofhouses)
       {
          printf("%s\n %s\n %d\n %d\n", &arrayofrecs[i].surname, &arrayofrecs[i].postcode, &arrayofrecs[i].rooms, &arrayofrecs[i].price);
          i++;
       }
    }
    void addhouse(House arrayofrecs[], int *noofhouses) {
    printf("\nAdd House\n");
    printf("Owner Surname: ");
    	scanf("%s", arrayofrecs[].surname);
    printf("\nPostcode: ");
    	scanf("%s", arrayofrecs[].postcode);
    printf("\nNumber of rooms: ");
    	scanf("%d", arrayofrecs[].rooms);
    printf("\nPrice: ");
    	scanf("%d", arrayofrecs[].price);
    printf("\nHouse added");
    
    List(list, noinlist);
    }
    
    void deletehouse(House arrayofrecs[], int noofhouses) {
    char deletesurname[20], yn;
    
    printf("\nDelete House\n");
    printf("Surname: ");
       scanf("%s", &deletesurname);
    
       printf("\nDelete file? (y/n): ");
    	scanf("%s", &yn);
    	if (yn == 'y')
    
    	List(list, noinlist);
    }
    
    
    
    }

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    Posting your errors would help, but from just glancing at your code:

    -main returns an int, not a void
    -I'm pretty sure you can't include functions in main the way you have there - they have to be defined outside of main.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: A little Problem!

    Originally posted by carter
    Would anybody be able to take a look and tell me why this code does not work
    Not until you explain what errors you get.

    void main ()
    You may start by changing this...

    fp=fopen("a:\\House.txt","r");
    printf("No on list?\n");
    fscanf(fp, "%d", noofhouses);
    Then add some error checking here. What would happen if fopen() fails, you think?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    6
    I got 2 errors - one was a 'declaration syntax error' and the other was 'missing declaration ;'

  5. #5
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    For those to be very useful, it'd help to see what lines they were on. I'd wager they're both caused because of the way you declared the functions.

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    one was a 'declaration syntax error'
    Probably due to the inside-main function declarations as Gov mentioned
    the other was 'missing declaration ;'
    Take a guess . Anyway, this error probably occur because of #1.

    (dang, beaten again . You're fast today, Gov).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    6
    O.K. The first one comes on this line

    Code:
    void read(House arrayofrecs[], int *noofhouses) {
    and the second one comes at the very end.

  8. #8
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    So, did you try what magos and I said? Define those functions outside of main.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>scanf("%s", arrayofrecs[].surname);
    What element number of the arrayofrecs array do you want to access? The compiler won't guess on your behalf.

    void main
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21