Thread: One more coding problem

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    6

    One more coding problem

    Code:
    This is what i have written so far but for some reason when passing the data back and forth from the getdata function it either does not work or does not record the correct numerical data, any help would be great
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    #define MAX_ELMNTS 100
    #define ANLYS_RNG 20
    
    typedef struct
    {
    char color[MAX_ELMNTS];
    char size[MAX_ELMNTS];
    int qty[ANLYS_RNG];
    double price[ANLYS_RNG];
    } PAINTDATA;
    
    void getdata (PAINTDATA paint1, PAINTDATA piant2, PAINTDATA paint3, PAINTDATA paint4);
    void printdata (PAINTDATA* pdata1,PAINTDATA* pdata2, PAINTDATA* pdata3, PAINTDATA* pdata4);
    //Functions in Other File
    
    int main()
    {
      PAINTDATA paint[4];
      PAINTDATA* pdata[4];
      pdata[4] = &paint[4];
      
      getdata (&paint[0], &paint[1], &paint[2], &paint[3]);
      
      printdata (&paint[0], &paint[1], &paint[2], &paint[3]);
      
      return 0;
    }
    
    void getdata (PAINTDATA paint1, PAINTDATA paint2, PAINTDATA paint3, 	PAINTDATA paint4)
    {
    	FILE* infile;
    
    	infile = fopen("paint.data", "r");
    	
    	if(infile == NULL)
    	{
    		printf ("There is no such file\n\n");
    		exit(1);
    	}
    	
    	fscanf(infile, "%s %s %d %lf", paint1.color, paint1.size, &paint1.qty, &paint1.price);
    	fscanf(infile, "%s %s %d %lf", paint2.color, paint2.size, &paint2.qty, &paint2.price);
    	fscanf(infile, "%s %s %d %lf", paint3.color, paint3.size, &paint3.qty, &paint3.price);
    	fscanf(infile, "%s %s %d %lf", paint4.color, paint4.size, &paint4.qty, &paint4.price);
    	
    	return;
    }
    
    void printdata (PAINTDATA* pdata1,PAINTDATA* pdata2, PAINTDATA* pdata3, PAINTDATA* pdata4)
    {
        
    	printf("%s %s %d %lf\n\n", pdata1->color, pdata1->size, pdata1->qty, pdata1->price);
    	printf("%s %s %d %lf\n\n", pdata2->color, pdata2->size, pdata2->qty, pdata2->price);
    	printf("%s %s %d %lf\n\n", pdata3->color, pdata3->size, pdata3->qty, pdata3->price);
    	printf("%s %s %d %lf\n\n", pdata4->color, pdata4->size, pdata4->qty, pdata4->price);
    	
    	return;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
      PAINTDATA paint[4];
      PAINTDATA* pdata[4];
      pdata[4] = &paint[4];
    Valid array indexes are 0 through size-1. Thus, 4 is actually beyond what you are allowed to manipulate.
    Code:
    for( x = 0; x < 4; x++ )
        pcdata[ x ] = &paint[ x ];

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. Replies: 16
    Last Post: 01-26-2006, 02:38 PM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM