Output problems with structures

This is a discussion on Output problems with structures within the C Programming forums, part of the General Programming Boards category; Sorry, I am a newbie and this is probably easy stuff for the rest of you. I need help with ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    10

    Output problems with structures

    Sorry, I am a newbie and this is probably easy stuff for the rest of you. I need help with my output table for my program. My output is coming out all messed up and I am at a loss on how to fix it. I know I am missing some needed code to figure out the experiment averages and the result averages, however I was trying to add one line of info at a time to make sure my input was coming out Ok (Also, nevermind the printf statements verifying input data, those are just for testing).

    If anyone could help me figure out why the following happens (and how to fix it):
    1). The result counter only displays "Result 1" for my output table.
    2). My rest of my result headers aren't displayinging correctly.
    3). The printf("--------"); statement is not displaying (separation line).
    4). And why the result header line prints at the end of the each line for Experiment names.

    I would be extremely grateful. Thanks

    ------------------------------------
    ***** output Table should look like:
    (well all lined up nice in columns anyhow)
    ------------------------------------


    Result 1 Result 2 Result 3 Result 4 Exp.Avg.
    ---------------------------------------------------------- ----------
    Fred: 1.0000 2.0000 3.0000 4.0000 2.5000
    Barney: 1.0000 3.0000 5.0000 7.0000 4.0000
    Betty: 11.0000 13.0000 17.0000 19.0000 15.0000
    ----------------------------------------------------------
    Result Avg: 4.3333 6.0000 8.3333 10.0000


    -------------------------------------
    ***** But, my output looks like this:
    -------------------------------------


    Result 1 Exp. Avg.
    Fred 1.0000 2.0000 3.0000 4.0000 Result 1 Exp. Avg.
    Barney 1.0000 3.0000 5.0000 7.0000 Result 1 Exp. Avg.
    Betty 11.0000 13.0000 17.0000 19.0000



    Code:
    #include <stdio.h>
    #include <stdlib.h> 
    #include <malloc.h>
    
    struct Result {
    	float value;                   /*  value of result */
    	struct Result *next;
    };
    
    
    struct Exp {
    	char ExpName[10];           
    	struct Result *pt_res;
    	struct Exp *next;
    };
    
    							/* Declare the list node to tie all 
    							the Experiment structs together into 
    							a list. */
    
        struct ExpList {
    	struct Exp *pExp;
    	struct ExpList *next;		/* --"next"-- was value */
    };
    
    
    /*  **************************************  */
    struct Exp* GetExp(int res)
    
    {
    	static int r = 0;			/* result counter, column control */
    	static int e = 0;   /*	experiment counter, row control */
    /*	int res;		 input result number, columns */
    /*	int exp;		 input experiment number, rows */
    
    	struct Exp *pNewExp = NULL;
    	struct Result *pTail = NULL;
    
    	pNewExp = (struct Exp *) malloc(sizeof(struct Exp)); 
    	pNewExp->pt_res = NULL;
    	pNewExp->next = NULL;
    
    		printf("\n");
    		printf("Please enter the name of Experiment %d: ", ++e);
    		gets(pNewExp->ExpName); 
    		
    		
    		printf("Experiment %d name is %s:", e, pNewExp->ExpName);
    		
    	for (r = 0; r < res; ++r)
    		{
    
    		struct Result *pNewRes = (struct Result *) malloc(sizeof(struct Result));
    		pNewRes->next = NULL;
    		printf("\n");
    		printf("Enter result %d for Experiment %s: ", r + 1, pNewExp->ExpName);
    		scanf("%f", &pNewRes->value); 
    		fflush(stdin); 
    
    		
    		printf("The result value for result %d of %s is %f:", r + 1, pNewExp->ExpName, pNewRes->value);
    		
    		
    			/* now build the linked list - this appends to the end of the list */
    			if ( pNewExp->pt_res == NULL ) {
    					pNewExp->pt_res = pNewRes;
    				} else {
    					pTail->next = pNewRes;
    				}
    				pTail = pNewRes;
    			}
    	printf("\n");
    	printf("\n");
    	printf("\n");
        return pNewExp;
    
    }
    
    /*  **************************************  */
    
    void PrintExp(struct Exp* pExp)
    
    {
    	int r = 0;
    	int res = 0;
    	int e = 0;
    	int exp = 0;  
    
    
        struct Result *pList = pExp->pt_res;
    	for (r = 0; r < res; ++r)
    
    		printf("\n\t\t");
    		printf("Result %d  ", ++r);
    		printf("Exp. Avg.");
    		printf("\n");
    		
    		for (r = 0; r < res; ++r)
    		{
    			printf("--------------");
    		}    
    	    printf("%s\t  ", pExp->ExpName);
    		while ( pList != NULL)
    			{
    				printf( "%8.4f ", pList->value);
    				pList = pList->next;
    			}  
    }
    
    /*  **************************************  */
    
    void PrintExpList(struct ExpList* pList)
    
    {
    		/* The first element in the list is a special case */
    		PrintExp(pList->pExp);
    	
    		while (pList->next)    /* --"next" was value --- */
    			 {
    			pList = pList->next; /* --"next" was value --- */
    			PrintExp(pList->pExp);
    		}
    }
    
    
    /*  **************************************  */
    
    void DeleteExp(struct Exp* pExp)
    
    {
       free(pExp);
    }
    
    
    /*  **************************************  */
    
    void DeleteExpList(struct ExpList* pList)
    
    {
       struct ExpList* pCursor;
    
       /* we have to treat the first node in the list as a special case */
       DeleteExp(pList->pExp);
    
       pList = pList->next;		/* --"next" was value --- */
    
       while (pList)
       {
          DeleteExp(pList->pExp);
          pCursor = pList;
          pList = pList->next;		/* --"next" was value --- */
    
          free(pCursor);
       }
    }
    
    
    /*  **************************************  */
    
    int main()
    {
       
       struct ExpList list; /* handles our list for the duration of main() */
       struct ExpList* pListTail; /* Util Pointer - help us build the list */
       struct ExpList* pNewListElement; /* a utility pointer for new list nodes */
       struct Exp* pNewExp;  /* a utility pointer for new data elements */
    
       /* declare some variables for the control logic */
       int res = 0;
       int e; 
       int exp; 
    /* int r;  */
    
       list.pExp = NULL;   /* Do some initialization to make our list safe (and empty) */
       list.next = NULL;	/* --"next" was value --- */
    
    	printf("How many Experiments are there?: ");
    	scanf("%d", &exp);
    	fflush(stdin);
    
    	printf("There are %d experiments:", exp);
    	printf("\n");
       
    	printf("How many Results are there?: ");
    	scanf("%d", &res);	
        fflush(stdin);
    
    	printf("There are %d results:", res);
    
      /* Now for each Experiment get a new record and add it to the list. */
      for (e = 0; e < exp; e++)       
       {
          pNewExp = GetExp(res);  /* Get a new data object from the user */
    
          if (list.pExp == NULL)
          {
             list.pExp = pNewExp;    /* first element in the list... */
             pListTail = &list;
          }
          else
          {		/* Make a new list node */
             pNewListElement = (struct ExpList*)malloc(sizeof(struct ExpList));
    		 pListTail->next = pNewListElement;	/* hook new list node to end of list */
    		 /* --"next" was value --- */	
             pListTail = pNewListElement; /* move tail marker to the new last node */
             pListTail->next = NULL;  /* new last node's next points to NULL */
    		 /* --"next" was value --- */
             pListTail->pExp = pNewExp;  /* new last node points to new Experiment record */
          }
       }
    
       PrintExpList(&list);   /* Print list of Experiments to screen */
    		printf("\n");
    		printf("\n");
    		printf("\n");
       DeleteExpList(&list); /* before exiting, free all the allocated memory */
    
       return 0;
    }

  2. #2
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    The problems seem to occuring within your PrintExp() function. Note that you declare the variable "res" in this function and initialise it to 0. You then try to use a for loop to print the Result heading which iterates from 0 to < res (which is zero at this point). Essentially this;

    for (r = 0; r < res; ++r)

    does nothing as the first time this is evaluated res is not less than 0 (it is equal to zero) and thus the code continues without iterating through this loop.

    Now the next problem is that when you don't enclose the appropriate code within braces, only the first line following the for loop is executed. So even if "res" was set to an appropriate value it would only execute the following lines of code the appropriate number of times, and everything else just the once:
    Code:
         for (r = 0; r < res; ++r)
              printf("\n\t\t");
    In order to fix the issue with the value of "res", I would recommend either passing the value of res to the function itself or incorporating a counter containing the number of results for each experiment in one of your structs.

    Also - you should not use fflush(stdin), this is undefined. You can use something along the lines of:

    while( getchar() != '\n' );
    Last edited by foniks munkee; 12-16-2002 at 04:32 AM.
    "Queen and huntress, chaste and fair,
    Now the sun is laid to sleep,
    Seated in thy silver chair,
    State in wonted manner keep."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Have a few minor problems trying to finish this program
    By kisiellll in forum C Programming
    Replies: 4
    Last Post: 02-22-2009, 06:00 PM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 02:08 PM
  3. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  4. Dialog LB output problems.
    By curlious in forum Windows Programming
    Replies: 3
    Last Post: 10-21-2003, 11:55 AM
  5. Minute Program output problems
    By Cobalt in forum C++ Programming
    Replies: 12
    Last Post: 10-12-2003, 10:16 AM

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