Hola a todos. Ante todo daros las gracia spor este maravilloso foro, por el cual podemos todos aprender cada día más y más.

Os comento mi problema. He estado haciendo un programa en C que no consigo hacerlo funcionar correctamente. El fallo es que al indicarle un valor de entrada, como por ejemplo "9", el programa me da un piso equivocado, el "2304". No consigo que me muestre bien el piso desde el que se tira el hijo. Lo he dado muchas vueltas pero no soy capaz de ver el fallo. Os agradeceria que me hecharais una mano. Muchas gracias:

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>


int main(void)
{
int fd;
int floor;

char buf[]="Legacy: Nothing I have, nothing I'll leave, nothing you'll have.\n\n";

// We create a file...
if ( (fd=creat("legacy.txt",0666))<0){
perror("error creating the file\n\n");
exit(1);
}
// ... and then we write the buffer on the file.
printf("Parent: I'm going to write my legacy...\n\n");
if ( write(fd,buf,64)!=64)
perror("Parent: D'oh! Some error happened while writing!\n\n");
else
printf("Parent: I have successfully written my legacy!\n\n");

int pid;
pid = fork();

if (pid < 0) { /* error occurred */
perror("Parent: The fork failed!\n\n");
exit(-1);
}

else if (pid==0) { /* child process */
printf("Child: I am the child, and I'm going to read my father's legacy... Mwahahaha! \n\n");
int n;
char buf[1];
fd=open("legacy.txt",O_RDONLY,0666);
while ((n=read(fd,buf,1))>0)
printf("%c",buf[0]);
printf("\n\n");
if (n<0) {
perror("Read error occured\n\n");
return -1;
}
}
else { /* parent process */
wait(&floor);
printf("Parent: How do you dare to read my papers!\n\n" );
}
if (pid==0) { /* child process */
printf("Child: How do you dare to leave me nothing! I cannot live since I know this...\n\n" );
printf("Advertisement: Hey! From which floor should the child jump? Text 'JUMP' to 7717 followed by the floor number.\n\n");
printf("Advertisement: Or you can just type it here:\n\n");
scanf("%d", &floor);
printf("\n\n");
exit(floor);
}
close(fd);
printf("Narrator: The whole family commited suicide.\n\n" );
printf("Narrator: Oh, by the way, the son jumped from the %dth floor.\n\n", floor);
return 0;

}


Muchas gracias.