[code]STU= (struct person_t ) malloc(sizeof(lperson_t))[code] that means that I should change this line to the malloc line right
Code:assert(STU != NULL);
Printable View
[code]STU= (struct person_t ) malloc(sizeof(lperson_t))[code] that means that I should change this line to the malloc line right
Code:assert(STU != NULL);
Is there anything here tha may have unallocated memory?Code:do
{
printf("\n\n"
"1. Enter new record\n"
"2. Display List\n"
"3. Quit\n\n");
printf("Prompt: ");
fflush(stdout);
if ( scanf("%d", &Ans) == 1 ) /* get answer */
{
fgets(STU->FName,sizeof(STU->FName),stdin);
if (STU == NULL)
{
perror("PERSON");
exit(EXIT_FAILURE);
}
is says to get it but it dose not allicatit. so i should change it toCode:fgets(STU->FName,sizeof(STU->FName),stdin);
so that it gets it and allicated it as wellCode:fgets(STU->FName),malloc(sizeof(STU->FName),stdin);
What??????
You seriously need to RTM on malloc().
Actually I'm starting to wonder if redmontab has ever studied a sorting algorithm before. Or if he's had any prior experience with C at all.
http://en.wikipedia.org/wiki/Merge_sort
im very new at this. And it is a little hard to understand on my own. THe book that i have dose not help me out to much. Some times it makes it worse.
You want to malloc an entire structure, so something like
to allocate a block the size of one structure.Code:malloc(sizeof(PERSON));
so I wnat the fgets to read the who ling. THe way I have it [code] fgets(STU->FName,sizeof(STU->FName),stdin);[code] I;m only reading the first name and not my whol structure. SO i need to change it to
this is because i thave my Structure person_T defined to call as PERSON. Is this correct. Soory if im not geeting this. I appreciate all of you help and patience with me :)Code:fgets(PERSON),malloc (sizeof(Person));
Also don't forget that malloc() can FAIL. So check the returned pointer BEFORE you use it!
Also look at calloc() if you want to allocate a block of 'n' structures. (And unlike malloc it will initialize the memory to zeroes too).
::sick of this::
Since you don't want to _LEARN_. . .
Insert this before your first fgets() as mentioned above.Code:STU = malloc(sizeof *STU);
if (STU == NULL){
printf("Out of memeory");
exit(0);
}
know i get the error 87 H:\lab4a.cpp invalid conversion from `void*' to `PERSON*'Code:STU = malloc(sizeof *STU);
It's actually :
Also, to get rid of that error, you want STU to be a void*. Then, when you use it, you can cast it to a (PERSON*) if you wish.Code:STU = malloc(sizeof(PERSON)); /* or whatever you called your structure */
if (STU == NULL){
printf("Out of memeory");
exit(0);
}
Stop compiling as C++.Quote:
Originally Posted by redmondtab
Quzah.
how do i do that. Im using the dev-c ++ complier
;) Im getting this error can someone help me please
117 H:\lab4a.cpp invalid conversion from `void*' to `PERSON*' the orange line is where im getting the error
Code:/****************************************************************
FILE NAME: lab4.cpp
DESCRIPTION: This program will ask the user if the want to read a file or input
data. Check the file to see if there is data if that is the first
request. IF not data go back to the options. Have user add data.
Send that data to an ordered linked list then save the data to a
file. Also have the options to show data from file or quit. .
DATE: October 22, 2006
DESIGNER: Tabatha Mitchell
ASSIGNMENT: Lab4
FUNCTIONS: PERSON* InsertNode (PERSON *Top, PERSON *Node )
amend_file
show_file
********************************************************************/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#define Nlen 26
typedef struct person_t { //structure that will sort that data for PERSONS
char FName[Nlen];
char LName[Nlen];
char Sex;
char Grade;
struct person_t *Next;
} PERSON;
struct person_t *Top = NULL; //used for the InsertNode set to empty
struct person_t *Node = NULL; //used for the InsertNode set to empty
/****************************************************************
FUNCTION: PERSON* InsertNode
DESCRIPTION: put the sturctured list into the ordered linked list
INPUT:
Parameters: The parameters as going to be the last name in the structured
list it is going to take the last name and sort it into an
ordered linked list
Keyboard: none
File: none
OUTPUT:
Return Value: last name in an ordered linked list
Parameters: last name ordered linked list
Display: none
File: none
CALLS TO: none
***************************************************************/
PERSON* InsertNode (PERSON *Top, PERSON *Node )
{
PERSON *Here, /* Node being compared to new node */
*Prev; /* Node in front of Here node */
if (Top == NULL)/* List is empty - Place new node at top of list */
Top = Node;
else { /* Top != NULL *//* Start at Top of list */
Here = Top;
strcmp (Here->LName , Node->LName);
Node->Next = Top;
Top = Node;
// else {/* Search for place to insert new node */
// Prev = Top;
// Here = Prev->Next;
while ( (Here != NULL) &&(Here->LName < Node->LName) ) {
Prev = Here;
Here = Here->Next;
}/* Insert into list at found spot */
Node->Next = Prev->Next;
Prev->Next = Node;
} /* end of Top != NULL */
return Top;
}
/*************************************************************
end of Function to Ordered linked list
***************************************************************/
void amend_file( char file[],PERSON *STU );//used to define amend_file function
void show_file( char file[],PERSON *STU ); //used to define show_file function
int main (void)
{
PERSON *STU;
int Ans;
char file[] = "data.txt";
int Records [100];
FILE *fin = fopen(file, "a+");
struct person_t *Top = NULL;
struct person_t *Node = NULL;
do
{
printf("\n\n"
"1. Enter new record\n"
"2. Display List\n"
"3. Quit\n\n");
printf("Prompt: ");
fflush(stdout);
if ( scanf("%d", &Ans) == 1 ) /* get answer */
{
STU = malloc (sizeof (person_t)); //allocate memory space for the STU
if (STU == NULL){ //if not enough space tell us out of memory
printf("Out of memory");
exit(0);
}
fgets(STU->FName,sizeof(STU->FName),stdin);
if (STU == NULL)
{
perror("PERSON");
exit(EXIT_FAILURE);
}
switch(Ans)
{
case 1:
amend_file(file, STU); //calls the amend file function
break;
case 2:
show_file(file, STU); //calls the show file function
break;
case 3: default:
Ans = 3;
break;
}
} /* /get answer */
}
while ( Ans != 3 );
fflush(stdout);
free(STU);
getchar ();
return EXIT_SUCCESS;
}
/****************************************************************
FUNCTION: amend_file
DESCRIPTION: this will get the input for the use, send it to the linked list
and then save it to the file.
INPUT:
Parameters: Person structure. and Person Insertnode
Keyboard: all of the following will be saved in the structure PERSON
LName= last name
FName = first name
Sex= what gender the person is
Grade = The persons grade
File: data.txt this is where the data is saved.
OUTPUT:
Return Value: Send The PERSONS STU to the InsertNode put data into list the
save to file
Parameters: InsertNode, LName, FName, Sex, Grade
Display: none
File: data.txt. the PERSONS STU in ordered link list format Lname, FName,
Sex, Grade
CALLS TO: List of all user-generated functions called
***************************************************************/
void amend_file( char file[], PERSON *STU )
{
int i;
assert(STU != NULL);
STU = (struct person_t *) malloc(sizeof(person_t));
FILE *fin = fopen(file, "a+");
if ( fin == NULL )
{
perror(file);
exit(EXIT_FAILURE);
}
printf("Enter new record: ");
printf ("\nEnter student's First name ( up to %d letters):", Nlen);
printf ("\nEnter student's Last name ( up to %d letters):", Nlen);
printf ("\nPlease enter Sex");
printf ("\nPlease enter Grade\n");
scanf("%s %s %c %c", STU->FName, STU->LName, &STU->Sex,
&STU->Grade);
InsertNode (Top, Node);
fprintf(fin, "%s %s %c %c\n", STU->LName, STU->FName, STU->Sex, STU->Grade);
fflush(fin);
fclose(fin);
}
/****************************************************************
FUNCTION: show_file
DESCRIPTION: Reads the data from the file and displays data after sorting if
needed.
INPUT:
Parameters: Person structure. and Person Insertnode these are read from the
file and then put into the list then displayed.
Keyboard:
File: data.txt this is where the data is saved.
OUTPUT:
Return Value: Send The PERSONS STU to the InsertNode put data into list the
save to file
Parameters: InsertNode, LName, FName, Sex, Grade
Display: the order list of people that are saved to the file.
File: data.txt. the PERSONS STU in ordered link list format Lname, FName,
Sex, Grade
CALLS TO:
***************************************************************/
void show_file ( char file[], PERSON *STU )
{
assert(STU != NULL);
int b = 1;
FILE *fin = fopen(file, "rb");
if (file == NULL)
{
perror(file);
exit(EXIT_FAILURE);
}
while (fscanf(fin, "%s %s %c %c", STU->FName, STU->LName, &STU->Sex,
&STU->Grade)== 4)
{
InsertNode (Top, Node);
b++;
printf("Record %d: %s %s %c %c\n", b, STU->FName, STU->LName,
STU->Sex, STU->Grade);
}
fclose(fin);
}