Please Help ?

This is a discussion on Please Help ? within the C Programming forums, part of the General Programming Boards category; Almost have this working but have some errors occuring--- error C2676: binary '[' : 'struct house' does not define this ...

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

    Please Help ?

    Almost have this working but have some errors occuring---

    error C2676: binary '[' : 'struct house' does not define this operator or a conversion to a type acceptable to the predefined operator
    error C2228: left of '.surname' must have class/struct/union type

    and the same 2 errors for each of postcode, room and price.

    [code]#include <stdio.h>
    #include <string.h>


    char surname[20] = {0}, postcode[7] = {0}, rooms[20] = {0}, price[6] ={0};
    int listchoice, house, cnt, totalcnt;
    FILE *fp, *outfile;


    struct house{
    char surname[20];
    char postcode[7];
    char rooms[20];
    char price[6];
    } house_details;


    int list(struct house house_details, int, char *);
    void addhouse(struct house house_details, int, char *);
    void deletehouse(struct house house_details, int, char *);
    void view(struct house house_details, int, char *);
    void save(struct house house_details, char *);



    int main (void)
    {

    char filename[25];

    printf("\n\nEnter file name to open: ");
    scanf("%s", &filename);
    fp=fopen(filename,"r");
    if (fp == NULL){printf("\nUnable to open file\n");return(0);}


    while(fscanf(fp, "%s %s %s %s", &house_details[cnt].surname, &house_details[cnt].postcode, &house_details[cnt].rooms, &house_details[cnt].price) != EOF) ++cnt;

    totalcnt = cnt;

    list(house_details, totalcnt, filename);

    return 0;
    }


    int list(struct house house_details, int totalcnt, char *filename){
    printf("\nHouse list:\n1) Add new record\n2) Delete record\n3) View phone book\n6);
    scanf("%d", &listchoice);

    switch (listchoice)
    {

    case 1: addrecord(house_details, totalcnt, filename); break;

    case 2: deleterecord(house_details, totalcnt, filename); break;


    case 3: view(house_details, totalcnt, filename); break;

    case 4: save(house_details, filename); return 0;
    }
    save(house_details, filename);
    return 0;
    }



    void addrecord(struct house house_details, int totalcnt, char *filename){
    printf("\nAdd Record\n");
    printf("Owner Surname: ");
    scanf("%s", house_details[totalcnt+1].surname);
    printf("\nPostcode: ");
    scanf("%s", house_details[totalcnt+1].postcode);
    printf("\nNumber of rooms: ");
    scanf("%s", house_details[totalcnt+1].rooms);
    printf("\nPrice: ");
    scanf("%s", house_details[totalcnt+1].price);
    printf("\nHouse added");
    ++totalcnt;
    list(house_details, totalcnt, filename);
    }

    void deleterecord(struct house house_details, int totalcnt, char *filename){
    char deletesurname[20], yn;

    printf("\nDelete Record\n");
    printf("Surname: ");
    scanf("%s", &deletesurname);



    printf("\nDelete file? (y/n): ");
    scanf("%s", &yn);
    if (yn == 'y'){
    }


    list(house_details, totalcnt, filename);
    }


    void view(struct house house_details, int totalcnt, char *filename){
    int i;
    printf("\nView Records\n");
    for (i=0;i<totalcnt;i++)
    {
    printf("\n%s %s %s %s", house_details[i].surname, house_details[i].postcode, house_details[i].rooms, house_details[i].price);
    }
    list(house_details, totalcnt, filename);
    }


    void save(struct house house_details, char *filename){

    outfile = fopen(filename, "w");
    fprintf(outfile, "%s", house_details);

    }[\code]

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    6
    Almost have this working but have some errors occuring---

    error C2676: binary '[' : 'struct house' does not define this operator or a conversion to a type acceptable to the predefined operator
    error C2228: left of '.surname' must have class/struct/union type

    and the same 2 errors for each of postcode, room and price.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    char surname[20] = {0}, postcode[7] = {0}, rooms[20] = {0}, price[6] ={0};
    int listchoice, house, cnt, totalcnt;
    FILE *fp, *outfile;
    
    
    struct house{
    char surname[20];
    char postcode[7];
    char rooms[20];
    char price[6];
    } house_details;
    
    
    int list(struct house house_details, int, char *);
    void addhouse(struct house house_details, int, char *);
    void deletehouse(struct house house_details, int, char *);
    void view(struct house house_details, int, char *);
    void save(struct house house_details, char *);
    
    
    
    int main (void)
    {
    
    char filename[25];
    
    printf("\n\nEnter file name to open: ");
    scanf("%s", &filename);
    fp=fopen(filename,"r");
    if (fp == NULL){printf("\nUnable to open file\n");return(0);}
    
    
    while(fscanf(fp, "%s %s %s %s", &house_details[cnt].surname, &house_details[cnt].postcode, &house_details[cnt].rooms, &house_details[cnt].price) != EOF) ++cnt;
    
    totalcnt = cnt;
    
    list(house_details, totalcnt, filename);
    
    return 0;
    }
    
    
    int list(struct house house_details, int totalcnt, char *filename){
    printf("\nHouse list:\n1) Add new record\n2) Delete record\n3) View phone book\n6);
    scanf("%d", &listchoice);
    
    switch (listchoice) 
    {
    
    case 1: addrecord(house_details, totalcnt, filename); break;
    
    case 2: deleterecord(house_details, totalcnt, filename); break;
    
    
    case 3: view(house_details, totalcnt, filename); break;
    
    case 4: save(house_details, filename); return 0;
    }
    save(house_details, filename);
    return 0;
    }
    
    
    
    void addrecord(struct house house_details, int totalcnt, char *filename){
    printf("\nAdd Record\n");
    printf("Owner Surname: ");
    scanf("%s", house_details[totalcnt+1].surname); 
    printf("\nPostcode: ");
    scanf("%s", house_details[totalcnt+1].postcode); 
    printf("\nNumber of rooms: ");
    scanf("%s", house_details[totalcnt+1].rooms);
    printf("\nPrice: ");
    scanf("%s", house_details[totalcnt+1].price); 
    printf("\nHouse added");
    ++totalcnt;
    list(house_details, totalcnt, filename);
    }
    
    void deleterecord(struct house house_details, int totalcnt, char *filename){
    char deletesurname[20], yn;
    
    printf("\nDelete Record\n");
    printf("Surname: ");
    scanf("%s", &deletesurname);
    
    
    
    printf("\nDelete file? (y/n): ");
    scanf("%s", &yn);
    if (yn == 'y'){ 
    }
    
    
    list(house_details, totalcnt, filename);
    }
    
    
    void view(struct house house_details, int totalcnt, char *filename){
    int i;
    printf("\nView Records\n");
    for (i=0;i<totalcnt;i++)
    {
    printf("\n%s %s %s %s", house_details[i].surname, house_details[i].postcode, house_details[i].rooms, house_details[i].price); 
    }
    list(house_details, totalcnt, filename);
    }
    
    
    void save(struct house house_details, char *filename){
    
    outfile = fopen(filename, "w");
    fprintf(outfile, "%s", house_details);
    
    }

  3. #3
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    Don't post the same thing twice, it's a waste. The problems are with how you defined your arrays. Unless there's something about structs that I don't know about, you normally cannot define something as char surname[20]={0}. Try defining each element of the array or assign values on a different line than the definition.

    Code:
    char surname[20]= {0,0,0,0,0,0,0,0,..../*for all 20 elements},...

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>you normally cannot define something as char surname[20]={0}
    Yes you can, but only if that's all it is. Initialising char array within a struct is similar too.

    >>printf("\nHouse list:\n1) Add new record\n2) Delete record\n3) View phone book\n6);
    This line has a quote missing from the end of the string.

    >>house_details
    This is a single struct, not an array of structs, so what are you up to here:
    >>&house_details[cnt].surname,

    >>addrecord / deleterecord
    These need prototypes.

    My compiler gave up in the end, sorry
    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

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