I can't figure out what is wrong with my code. I get no compiler errors, but my program crashes when a number is entered for "Please enter result # for Experiment name:"
Can anyone figure out what I am doing wrong? Please help!
Code:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct Result {
float value; /* value of result */
struct Result *pt_res_next;
};
struct Exp {
char ExpName[10];
struct Result *pt_res;
struct Exp *pt_exp_next;
};
/* Declare the list node to tie all the Experiment structs together into
a list.
*/
struct ExpList
{
struct Exp* pExp;
struct ExpList* value;
};
struct Exp* GetExp()
/*
Assumes: ???????
Results: Allocates a new Experiment structure and asks the user for the
values to fill the Experiment fields (Experiment name and result number).
Returns: A pointer to a new struct Experiment*. It is the responsibility
of the caller to delete this struct.
*/
{
int i;
int ExpResult;
struct Exp* pNewExp;
struct Result* pNewRes;
pNewExp = (struct Exp*)malloc(sizeof(struct Exp));
printf("How many results for Experiment? :");
scanf("%d", &ExpResult);
getchar();/* this getchar eats the return left by the scanf. */
for (i = 0; i < ExpResult; ++i)
{
printf("Please enter the name of the Experiment:");
gets(pNewExp->ExpName);
pNewRes = (struct Result*)malloc(sizeof(struct Result));
printf("Please enter result %d for Experiment %s :", i + 1, pNewExp->ExpName);
/* pNewValue = (struct Result*)malloc(sizeof(struct Result)); */
scanf("%f", pNewRes->value);
getchar();/* this getchar eats the return left by the scanf. */
}
return pNewExp;
}
void PrintExp(struct Exp* pExp)
/*
Assumes: ???????
Results: Prints the name and value number fields of the Experiment struct
Returns: none
*/
{
printf("Experiment name%s\n", pExp->ExpName);
printf("Result: %f\n", pExp->pt_res);
}
void PrintExpList(struct ExpList* pList)
/*
Assumes: ???????
Results: Prints the name and phone number for every Experiment in the list
pList. The Experiment records are seperated on the screen by a
blank line.
Returns: none
*/
{
/* The first element in the list is a special case */
PrintExp(pList->pExp);
while (pList->value)
{
pList = pList->value;
PrintExp(pList->pExp);
}
}
void DeleteExp(struct Exp* pExp)
/*
Assumes: ???????
Results: frees all memory allocated for the Experiment struct pointed to
by pEmp.
Returns: none
*/
{
free(pExp);
}
void DeleteExpList(struct ExpList* pList)
/*
Assumes: ???????
Results: Deletes every Experiment in the list pList and every ExperimentList
struct except the first one.
Returns: none
*/
{
struct ExpList* pCursor;
/* we have to treat the first node in the list as a special case */
DeleteExp(pList->pExp);
pList = pList->value;
while (pList)
{
DeleteExp(pList->pExp);
pCursor = pList;
pList = pList->value;
free(pCursor);
}
}
int main()
{
/* this is our list handle for the duration of main() */
struct ExpList list;
/* a utility pointer to help us build the list */
struct ExpList* pListTail;
/* a utility pointer for new list nodes */
struct ExpList* pNewListElement;
/* a utility pointer for new data elements */
struct Exp* pNewExp;
/* declare some variables for the control logic */
int nNumExps;
int i;
/* Do some initialization to make our list safe (and empty) */
list.pExp = NULL;
list.value = NULL;
/* Ask the user how many Exps to enter into the list */
printf("How many Experiments are there?: ");
scanf("%d", &nNumExps);
getchar();/* this getchar eats the return left by the scanf. */
/* Now for each Experiment get a new record and add it to the list. */
for (i = 0; i < nNumExps; i++)
{
/* Get a new data object from the user */
pNewExp = GetExp();
if (list.pExp == NULL)
{
/* This is the first element in the list... */
list.pExp = pNewExp;
pListTail = &list;
}
else
{
/* there are already elements in the list (at least one),
so add this one to the end */
/* Step one is to make a new list node */
pNewListElement = (struct ExpList*)malloc(sizeof(struct ExpList));
/* Now hook that new list node up to the end of the list */
pListTail->value = pNewListElement;
/* then move the tail marker to the new last node */
pListTail = pNewListElement;
/* Now make sure the new last node's next points to NULL so we can
find the end of the list */
pListTail->value = NULL;
/* and lastly, make the new last node point at the new Experiment
record. */
pListTail->pExp = pNewExp;
}
}
/* Print the list of Experiments out to the screen */
PrintExpList(&list);
/* The last thing we need to do before we exit is to free all the memory we
allocated. */
DeleteExpList(&list);
return 0;
}
Tagged by Salem