Thread: Compile error?????

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    34

    Compile error?????

    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

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    choice should be declared at the beginning of main. and so should pid_t r1pid. Of course with r2pid. All variables are declared at a beginning of a function. Solves all my errors I get. You need to include <stdlib.h> for srand, rand, and exit. and return 0 at the end of main. Also throw is c++
    Last edited by linuxdude; 06-19-2004 at 10:03 PM.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Also throw is c++
    Well actually, 'throw' in this case is a function they've written.
    Also, C99 allows you to declare variables scattered around in unsightly fashion, IIRC. But, there's an easier fix if they just want the variables declared right there:
    Code:
    case foo:
    { 
        int x;
        ...do whatever...
    }
    It's legal to declare variables any time you have a new scope block. So, by providing a new one around them, you're allowed to declare your variables there.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    IIRC there is a problem with declaring a variable inside a case. I don't remember correctly the exact issue but I do remember it being a PITA enough that I avoid it whenever possible.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > IIRC there is a problem with declaring a variable inside a case
    You're stuck between a rock and a hard place basically.
    Code:
    int main(int argc, char **argv) {
        switch ( argc ) {
            int foo;   /* warning: `foo' might be used uninitialized in this function */
            int bar = 0; /* warning: unreachable code at beginning of switch statement */
            case 0:
                printf( "%d\n", foo );
                break;
            default:
                printf( "%d\n", bar );
                break;
        }
        return 0;
    }
    If the compiler ever starts giving you "may be used uninitialised", then there is no way to initialise it in the declaration. Then you might have to start getting really creative to make the uninitialised warning go away

    > srand(pid);
    > return rand()%3;
    This probably isn't much better than just returning pid%3
    Some really poor random number generators return seed on the first call, so this is exactly pid%3
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    ---
    Join Date
    May 2004
    Posts
    1,379
    Quote Originally Posted by Salem
    return rand()%3;
    This probably isn't much better than just returning pid%3
    Some really poor random number generators return seed on the first call, so this is exactly pid%3
    right, you should seed it with the current time
    Code:
    #include <time.h>
    int main(void)
    {
      srand(time(NULL));
      int rnd;
    
      rnd = rand();
    
      return 0;
    }

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >srand(time(NULL));
    While this will most likely work, it's not portable.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    34
    I was going to use time, however it was pointed out that on some unix machines the child process would screw up somehow, but thank you for the the tip, am thinking of making some function with the pid and the time combined ?

    am goign to try ur suggestions on fixing the program thank you very much

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > right, you should seed it with the current time
    Look at the code, and tell me how two processes created in quick succession would ever have a different value of time() ?
    They might, but that would be highly exceptional.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 07:17 PM
  4. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM