Data structure declaration error

This is a discussion on Data structure declaration error within the C Programming forums, part of the General Programming Boards category; This code is a part of program, which consolidates variables in array of structures from many records to only a ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    123

    Data structure declaration error

    This code is a part of program, which consolidates variables in array of structures from many records to only a few records in one dimension array.
    I have a declaration problem which somehow I cannot manage to debug what-so-ever...
    Code:
    	
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #define TOTAL_RENTALS 5
    #define ALL_CARS 4
    
    typedef enum areas{north=1, center, south} areas;
    
    typedef struct {
    	int model;
    	areas area;
    	int rental_length;
    	int day_pay;
    	int age;
    } rentals;
    
    typedef struct {
    	int model;
    	char mnfctr[20];
    	char model_name[15];
    	int stock;
    } car;
    
    	int process[TOTAL_RENTALS]={0};
    	int sum_rental_length[TOTAL_RENTALS];
    	void consolidate(int sum_rental_length[TOTAL_RENTALS]);
    
    	
    void main()
    {
    	
    	int i;
    	rentals all_rentals[TOTAL_RENTALS]=
    	{
    		{1111, north, 111, 20, 23},
    		{2222, south, 010, 25, 35},
    		{3333, center, 016, 23, 39},
    		{4444, north, 011, 25, 57},
    		{2222, center, 001, 23, 45},
    	};
    	
    	car cars[ALL_CARS]=
    	{
    		{1111, "Rols-Roys", "R-1", 010},
    		{2222, "Mercedes", "M-1", 020},
    		{3333, "Mercedes", "M-2", 057},
    		{4444, "Jajuar", "J-1", 103},
    	};
    
    consolidate (sum_rental_length);
    
      for(i=0;i<TOTAL_RENTALS;i++)
    	{
    		if(sum_rental_length[i]!=0)
    			printf("For Catalog %d The Sum is %d\n", rentals.model[i],sum_rental_length[i]);
    	}
    }
    
    
    	void consolidate(int sum_rental_length[TOTAL_RENTALS])
    {
    	int  i,j;
        for(i=0;i<TOTAL_RENTALS;i++)
    	{
    		if(!process[i])
    		{
    			sum_rental_length[i]+=rentals.rental_length[i]; 
    			for(j=i+1;j<TOTAL_RENTALS;j++)
    			{
    				if(rentals.model[i]==rentals.model[j])
    				{
    					sum_rental_length[i]+=rentals.rental_length[j];
    					process[j]=1;
    				}
    			}
    		}
    
    }
    Any help will be highly appreciated!

    compiler says:

    : error C2059: syntax error : 'type'
    : error C2275: 'rentals' : illegal use of this type as an expression
    : see declaration of 'rentals'
    : error C2059: syntax error : 'type'
    Last edited by ronenk; 09-30-2004 at 02:01 PM.

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    84
    rentals is type. all_rentals is array. So
    Code:
    rentals.model[i]
    should be
    Code:
    all_rentals[i].model
    There's other exatly the same bugs in your code. Also all_rentals is declared in main, but you using it in consolidate() function, so ether declare it as global (not recommended) or pass pointer in consolidate() function (recommended).

    EDIT: end you missing '}' at the end

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Here's some fixed code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define TOTAL_RENTALS 5
    #define ALL_CARS      4
    
    typedef enum areas {north = 1, center, south} areas;
    
    typedef struct
    {
      int model;
      areas area;
      int rental_length;
      int day_pay;
      int age;
    } rentals;
    
    // all_rentals must be global or you need to pass it to consolidate
    rentals all_rentals[TOTAL_RENTALS] =
    {
      { 1111, north,  111, 20, 23 },
      { 2222, south,  010, 25, 35 },
      { 3333, center, 016, 23, 39 },
      { 4444, north,  011, 25, 57 },
      { 2222, center, 001, 23, 45 }
    };
    /*  This car struct is never even used
    typedef struct
    {
      int model;
      char mnfctr[20];
      char model_name[15];
      int stock;
    } car;
    */
    int process[TOTAL_RENTALS] = { 0 };
    int sum_rental_length[TOTAL_RENTALS];
    void consolidate(int sum_rental_length[TOTAL_RENTALS]);
    
    // return type of main() must be int
    int main(void)
    {
      int i;
    // Oncea gain, cars is never used
    /*  car cars[ALL_CARS] =
      {
        { 1111, "Rols-Roys", "R-1", 010 },
        { 2222, "Mercedes",  "M-1", 020 },
        { 3333, "Mercedes",  "M-2", 057 },
        { 4444, "Jajuar",    "J-1", 103 }
      };
    */
      consolidate(sum_rental_length);
    
      for(i = 0;i < TOTAL_RENTALS;i++)
      {
        if(sum_rental_length[i] != 0)
          printf("For Catalog %d The Sum is %d\n",
            all_rentals[i].model, sum_rental_length[i]);
    // rentals is a type, all_rentals actually has space reserved. When you have an array of
    // structs you do it like array[i].member, but if you have an array inside a struct you do
    // it like struct.memberarray[i]
      }
    
      return 0;
    }
    
    void consolidate(int sum_rental_length[TOTAL_RENTALS])
    {
      int i, j;
    
      for(i = 0;i < TOTAL_RENTALS;i++)
      {
        if(!process[i])
        {
          sum_rental_length[i] += all_rentals[i].rental_length;
          for(j = i+1;j < TOTAL_RENTALS;j++)
          {
            if(all_rentals[i].model == all_rentals[j].model)
            {
              sum_rental_length[i] += all_rentals[j].rental_length;
              process[j] = 1;
            }
          }
        }
      }
    }
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    this is after the modifications you offered:
    Code:
    void consolidate(int sum_rental_length[TOTAL_RENTALS], rentals *all_rentals_ptr);
    	rentals *all_rentals_ptr;
    	
    void main()
    {
    	
    	int i;
    	rentals all_rentals[TOTAL_RENTALS]=
    	{
    		{1111, north, 111, 20, 23},
    		{2222, south, 010, 25, 35},
    		{3333, center, 016, 23, 39},
    		{4444, north, 011, 25, 57},
    		{2222, center, 001, 23, 45},
    	};
    
    	all_rentals_ptr=all_rentals;
    
    	car cars[ALL_CARS]=
    	{
    		{1111, "Rols-Roys", "R-1", 010},
    		{2222, "Mercedes", "M-1", 020},
    		{3333, "Mercedes", "M-2", 057},
    		{4444, "Jajuar", "J-1", 103},
    	};
    
    consolidate (sum_rental_length, all_rentals_ptr);
    
      for(i=0;i<TOTAL_RENTALS;i++)
    	{
    		if(sum_rental_length[i]!=0)
    			printf("For Catalog %d The Sum is %d\n", all_rentals_ptr->model[i],sum_rental_length[i]);
    	}
    }
    
    
    	void consolidate(int sum_rental_length[TOTAL_RENTALS], rentals *all_rentals_ptr)
    {
    	int  i,j;
        for(i=0;i<TOTAL_RENTALS;i++)
    	{
    		if(!process[i])
    		{
    			sum_rental_length[i]+=all_rentals_ptr->rental_length[i]; 
    			for(j=i+1;j<TOTAL_RENTALS;j++)
    			{
    				if(all_rentals_ptr->model[i]==all_rentals_ptr->model[j])
    				{
    					sum_rental_length[i]+=all_rentals_ptr->rental_length[j];
    					process[j]=1;
    				}
    			}
    		}
    
    }
    Still got errors on lines of function consolidate :
    error C2109: subscript requires array or pointer type

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    This is wrong:
    Code:
    printf("For Catalog %d The Sum is %d\n", all_rentals_ptr->model[i],sum_rental_length[i]);
    Read the note in the code I posted.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    I would like to reveal now another part of the program which, eventually has to find the most rented car model, name & manufacturer out of rentals struct. In order to do that I want to sort array sum_rental_length, which summarize all rentals & take the indexes of highest value (if more then one- all of them) & print them out.
    The problem are:
    1. The sorting algorithm, though I think it good, but for some reason array is not sorted.
    2. How do take the model no. found in sorting action & find the model name & manufacturer?

    TIA,

    Ronen

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define TOTAL_RENTALS 6
    #define ALL_CARS      4
    
    typedef enum areas {north = 1, center, south} areas;
    
    typedef struct
    {
      int model;
      areas area;
      int rental_length;
      int day_pay;
      int age;
    } rentals;
    
    rentals all_rentals[TOTAL_RENTALS] =
    {
      { 1111, north,  011, 20, 23 },
      { 2222, south,  015, 25, 35 },
      { 3333, center, 016, 23, 39 },
      { 4444, north,  011, 25, 57 },
      { 2222, center, 010, 23, 45 },
      { 4444, center, 003, 20, 52 },
    };
    
    typedef struct
    {
      int model;
      char mnfctr[20];
      char model_name[15];
      int stock;
    } car;
    car cars[ALL_CARS] =
      {
        { 1111, "Rols-Roys", "R-1", 010 },
        { 2222, "Mercedes",  "M-1", 020 },
        { 3333, "Mercedes",  "M-2", 057 },
        { 4444, "Jajuar",    "J-1", 103 }
      };
    
    int process[TOTAL_RENTALS] = { 0 };
    int sum_rental_length[TOTAL_RENTALS]={0};
    void hi_rent_model(int sum_rental_length[TOTAL_RENTALS], car cars[ALL_CARS]);
    void title (void);
    
    int main(void)
    {
      hi_rent_model(sum_rental_length, cars);
    
      
      return 0;
    }
    
    void hi_rent_model(int sum_rental_length[TOTAL_RENTALS], car cars[ALL_CARS])
    {
      int i, j,temp;
    
      for(i = 0;i < TOTAL_RENTALS;i++)
      {
        if(!process[i])
        {
          sum_rental_length[i] += all_rentals[i].rental_length;
          for(j = i+1;j < TOTAL_RENTALS;j++)
          {
            if(all_rentals[i].model == all_rentals[j].model)
            {
              sum_rental_length[i] += all_rentals[j].rental_length;
              process[j] = 1;
            }
          }
        }
      }
    
    	for  (i=0;i<TOTAL_RENTALS;++i)
    		for (j=i+1;j>TOTAL_RENTALS;++j)
    	{
    
    		if (sum_rental_length[i]> sum_rental_length[j])
    		{
    			temp=sum_rental_length[j];
    			sum_rental_length[j]=sum_rental_length[i];
    			sum_rental_length[i]=temp;
    		}
    
    		
    	}
    		for(i = 0;i < TOTAL_RENTALS;i++)
      {
        if(sum_rental_length[i] != 0)
          printf("Model %d had a total of %d days\n", all_rentals[i].model, sum_rental_length[i]);
      }
    
      	title();
    	printf ("\n %2d %17s %13s %8d\n ",cars[0].model, cars[0].model_name, cars[0].mnfctr, sum_rental_length[0] );
    }
    
    
    
    void title (void)
    {
    	puts ("============================================================\n\r");
    	puts ("model number | model name | manufacturer | total rental days\n\r");
    	puts ("============================================================\n\r");
    }

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    You're going outside of array bounds.
    Code:
    	for  (i=0;i<TOTAL_RENTALS;++i)
    		for (j=i+1;j>TOTAL_RENTALS;++j)
    Did you mean to use octal values?
    Code:
    rentals all_rentals[TOTAL_RENTALS] =
    {
      { 1111, north,  011, 20, 23 },
      { 2222, south,  015, 25, 35 },
      { 3333, center, 016, 23, 39 },
      { 4444, north,  011, 25, 57 },
      { 2222, center, 010, 23, 45 },
      { 4444, center, 003, 20, 52 },
    };
    In the function hi_rent_model, can you tell me which is being used: the global or the passed parameter?
    Code:
    car cars[ALL_CARS] =
      {
        { 1111, "Rols-Roys", "R-1", 010 },
        { 2222, "Mercedes",  "M-1", 020 },
        { 3333, "Mercedes",  "M-2", 057 },
        { 4444, "Jajuar",    "J-1", 103 }
      };
    
    int sum_rental_length[TOTAL_RENTALS]={0};
    
    /* ... */
    
    void hi_rent_model(int sum_rental_length[TOTAL_RENTALS], car cars[ALL_CARS])
    You may want to choose different names to make this more clear.
    Last edited by Dave_Sinkula; 10-01-2004 at 10:44 AM. Reason: Fixed color tags.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    You're going outside of array bounds.
    Code:
    for (i=0;i<TOTAL_RENTALS;++i)
    for (j=i+1;jCOLOR=Teal]>[/COLOR]TOTAL_RENTALS;++j)
    fixed in my code.


    Did you mean to use octal values?
    Code:
    rentals all_rentals[TOTAL_RENTALS] =
    {
    { 1111, north, 011, 20, 23 },
    { 2222, south, 015, 25, 35 },
    { 3333, center, 016, 23, 39 },
    { 4444, north, 011, 25, 57 },
    { 2222, center, 010, 23, 45 },
    { 4444, center, 003, 20, 52 },
    };
    it was just ocasional...I meant decimal notation.


    In the function hi_rent_model, can you tell me which is being used: the global or the passed parameter?
    Code:
    car cars[ALL_CARS] =
    {
    { 1111, "Rols-Roys", "R-1", 010 },
    { 2222, "Mercedes", "M-1", 020 },
    { 3333, "Mercedes", "M-2", 057 },
    { 4444, "Jajuar", "J-1", 103 }
    };

    int sum_rental_length[TOTAL_RENTALS]={0};

    /* ... */

    void hi_rent_model(int sum_rental_length[TOTAL_RENTALS], car cars[ALL_CARS])
    After looking again at my code I can see that all arrays & structs are global. I know it's not ideal, but it make it simpler for me...
    Last edited by ronenk; 10-01-2004 at 11:08 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 07:13 PM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM

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