freedon
03-06-2004, 12:14 AM
First I'll explain what I need to do. I have to use pointers with Dinamic Memory.
This is schoolwork. The purpose is have a code of the registration of the clients on a hotel.
(the code will be on the bottom)
First, ask how many rooms does the hotel have. Then using a for loop, create new nods (In spanish we call it nodos, I guess NOD is the translation to english). Inside the loop, create new nod and fill it with data [the room equals to the same number as t (the variable I use to control the loop). But the other data, name, address, sex, phone number, etc I want to put NULL. At first I tried filling it with just a string of data {"----"}, didn't work, so later I declared a variable that had {"----"} and I equalled name, address, etc to that variable, didn't even compile. So I'm stuck there, I'm almost done with the rest of the code.
typedef struct nodo
{
int cuarto; //room
char nombre[30]; //name
char dir[30]; //address
char tel[8]; //phone number
char sexo; //sex
struct nodo *enlace; //enlace is used to connect nods
}lista;
lista *ptr=NULL;
lista *p1,*p2,*p3,*prin,*nuevo;
int opmain, num=0,total ,t=0 ,opcuarto ,opreportecliente ,opreporte1 ,opregistro ,opmain2;
char sex, vacio[4] = {"----"},vacio2[1] = {'-'};
MAIN CODE
void main(void)
{
The main code does work, too long to post :p
call function crearcuartos (create rooms)
}
void crearcuartos()
{
if(total!=0||ptr!=NULL)
printf("\nRooms have already been defined");
else
{
printf("\nHow many rooms in the hotel: ");
scanf("%d",&num);
for(t=0;t<num;t++)
{
nuevo=(lista*)malloc(sizeof(lista));
flushall();
nuevo->cuarto=(num-t);
flushall();
nuevo->nombre[t]=vacio2[t];
flushall();
nuevo->dir[t]=vacio[t];
flushall();
nuevo->tel=NULL;
flushall();
nuevo->sexo='-';
if(ptr==NULL) //if nod is the first created
{
nuevo->enlace=NULL; //new nod of connect is equal to NULL
ptr=nuevo; //ptr (main pointer) equals new nod position
}
else
{
nuevo->enlace=ptr; //new nod connects to ptr
ptr=nuevo; //ptr equals new position
}
}
} //**end function 'create rooms'
As you can see in this part
flushall();
nuevo->nombre[t]=vacio2[t];
flushall();
nuevo->dir[t]=vacio[t];
flushall();
nuevo->tel=NULL;
I tried 3 different ways, equal to the value of vacio2, vacio and NULL, neither work. NULL doesn't even compile, I get a "Lvalue requiered"
flushall();
nuevo->sexo='-';
this one does work
Hope someone can help me before I go crazy :o
This is schoolwork. The purpose is have a code of the registration of the clients on a hotel.
(the code will be on the bottom)
First, ask how many rooms does the hotel have. Then using a for loop, create new nods (In spanish we call it nodos, I guess NOD is the translation to english). Inside the loop, create new nod and fill it with data [the room equals to the same number as t (the variable I use to control the loop). But the other data, name, address, sex, phone number, etc I want to put NULL. At first I tried filling it with just a string of data {"----"}, didn't work, so later I declared a variable that had {"----"} and I equalled name, address, etc to that variable, didn't even compile. So I'm stuck there, I'm almost done with the rest of the code.
typedef struct nodo
{
int cuarto; //room
char nombre[30]; //name
char dir[30]; //address
char tel[8]; //phone number
char sexo; //sex
struct nodo *enlace; //enlace is used to connect nods
}lista;
lista *ptr=NULL;
lista *p1,*p2,*p3,*prin,*nuevo;
int opmain, num=0,total ,t=0 ,opcuarto ,opreportecliente ,opreporte1 ,opregistro ,opmain2;
char sex, vacio[4] = {"----"},vacio2[1] = {'-'};
MAIN CODE
void main(void)
{
The main code does work, too long to post :p
call function crearcuartos (create rooms)
}
void crearcuartos()
{
if(total!=0||ptr!=NULL)
printf("\nRooms have already been defined");
else
{
printf("\nHow many rooms in the hotel: ");
scanf("%d",&num);
for(t=0;t<num;t++)
{
nuevo=(lista*)malloc(sizeof(lista));
flushall();
nuevo->cuarto=(num-t);
flushall();
nuevo->nombre[t]=vacio2[t];
flushall();
nuevo->dir[t]=vacio[t];
flushall();
nuevo->tel=NULL;
flushall();
nuevo->sexo='-';
if(ptr==NULL) //if nod is the first created
{
nuevo->enlace=NULL; //new nod of connect is equal to NULL
ptr=nuevo; //ptr (main pointer) equals new nod position
}
else
{
nuevo->enlace=ptr; //new nod connects to ptr
ptr=nuevo; //ptr equals new position
}
}
} //**end function 'create rooms'
As you can see in this part
flushall();
nuevo->nombre[t]=vacio2[t];
flushall();
nuevo->dir[t]=vacio[t];
flushall();
nuevo->tel=NULL;
I tried 3 different ways, equal to the value of vacio2, vacio and NULL, neither work. NULL doesn't even compile, I get a "Lvalue requiered"
flushall();
nuevo->sexo='-';
this one does work
Hope someone can help me before I go crazy :o