![]() |
| | #1 |
| Registered User Join Date: Oct 2002
Posts: 10
| Output problems with structures 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;
}
|
| Gkitty is offline | |
| | #2 |
| Registered User 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");
Also - you should not use fflush(stdin), this is undefined. You can use something along the lines of: while( getchar() != '\n' );
__________________ "Queen and huntress, chaste and fair, Now the sun is laid to sleep, Seated in thy silver chair, State in wonted manner keep." Last edited by foniks munkee; 12-16-2002 at 05:32 AM. |
| foniks munkee is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Have a few minor problems trying to finish this program | kisiellll | C Programming | 4 | 02-22-2009 07:00 PM |
| Help for my output array | qwertysingh | C Programming | 1 | 02-17-2009 03:08 PM |
| Formatting output into even columns? | Uncle Rico | C Programming | 2 | 08-16-2005 05:10 PM |
| Dialog LB output problems. | curlious | Windows Programming | 3 | 10-21-2003 11:55 AM |
| Minute Program output problems | Cobalt | C++ Programming | 12 | 10-12-2003 10:16 AM |