Hi guys and girls,
i have this code, it was working and the next save it dosnt work and i only added comments >_<
cant spot the problem can an experienced eye spot it?
here is the code
Code:
#include<stdio.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
/* Notes: */
/* What should the seed be for best results? */
/* Program to simulate the game of rock scissors paper. */
int main(){
int pid,pid2,status,status2;
/* This switch controls the first fork */
switch (pid = fork()) {
/* if pid for the current process is 0 then it is the forked first child */
case 0:
int choice = throw(getpid());
exit(choice);
/* this break; will lead the first child out of the entire double switch structure preventing it from the parent code. */
break;
case -1:
/* errno is set, so pritn it with perror for this child and the second one */
printf("Fork failed!");
break;
/* Otherwise it is the parent; who will yet again fork another child */
default:
switch (pid2 = fork()) {
/* if pid for the current process is 0 then it is the forked second child */
case 0:
printf("\nHello I am the second child running :D, and my pid is: %d\n",getpid());
int choice = throw(getpid());
exit(choice);
break;
case -1:
/* print errno */
printf("Fork 2 failed!");
break;
default:
pid_t r1pid = wait(&status);
/* WEXITSTATUS(status) */
pid_t r2pid = wait(&status2);
printf("The slowest child returns with code: %i , and pid : %d !\n",WEXITSTATUS(status2), r2pid);
break;
}
break;
}
}
int throw(pid_t pid){
srand(pid);
return rand()%3;
}
and here is the errors i get:
a2t1.c:16: error: parse error before "int"
a2t1.c:17: error: `choice' undeclared (first use in this function)
a2t1.c:17: error: (Each undeclared identifier is reported only once
a2t1.c:17: error: for each function it appears in.)
a2t1.c:38: error: parse error before "r1pid"
thank you i advance