#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    C Simple child and parent back and forth


    Im creating a small guess game between a child and a parent: The child takes in the input from user and makes that the number to guess, the parent simply goes into a loop trying to figure out which is the right number.
    I can't get it to work fully. Also you can see I'm keeping track of how many times it takes, once its found, make a new one and then exits after 4 rounds.


    Code:
    #include <stdio.h> 
    #include <unistd.h> 
    #include <time.h> 
    #include <stdlib.h>  
    char readbuffer[100]; 
    
     int main() {  
        int pid, nbytes, i, input;   
        int fd[2] = {} ;   
        int guess = 0;   
        pid = fork();  
        int x;   
        
         srand((unsigned) time(NULL));  
         x = (rand() % 10 + 1);  
    
           if (pid == 0) 
        {     
           close(fd[0]);    
           printf("enter a number ");     
           scanf("%d", &input); 
          
           write(fd[1], &input, (sizeof(input)));
          
            if (x == input) 
            {    
                exit(0);     
            }  
    
          close(fd[1]);  
       }
     
      else 
      {   
         //PARENT     
        close(fd[1]);    
        nbytes = read(fd[0], &input, sizeof(input));  
       
        while (x != input)
        {    
          if (x != input) 
          {        
             guess++;       
          }     
        }   
          char answer; 
       
        if (x == input) 
       {    
          printf(" correct");       
          printf("Number of Guesses: %d\n", guess);
           exit(0);    
        }    
    
     //to reset random   
     
        x = (rand() % 10 + 1);  
       close(fd[0]);   } }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    For one thing, format your code! The reason to use code tags is so that your code will not lose its formatting. That jumbled mess that you posted is worse than not using code tags. Whatever demon possessed you to hide all your braces at the ends of lines like that?

    Code:
    #include <stdio.h> 
    #include <unistd.h> 
    #include <time.h> 
    #include <stdlib.h>  
    
    char readbuffer[100]; 
    
    int main() 
    {  
        int pid, nbytes, i, input;   
        int fd[2] = {} ;   
        int guess = 0;   
        pid = fork();  
        int x;   
        srand((unsigned) time(NULL));  
        x = (rand() % 10 + 1);  
        if (pid == 0) 
        {     
            close(fd[0]);    
            printf("enter a number ");     
            scanf("%d", &input); 
            write(fd[1], &input, (sizeof(input)));
            if (x == input) 
            {    
                exit(0);     
            }  
            close(fd[1]);  
        } 
        else 
        {   
            //PARENT     
            close(fd[1]);    
            nbytes = read(fd[0], &input, sizeof(input));  
            while (x != input) 
            {    
                if (x != input) 
                {        
                    guess++;       
                }     
            }   
            char answer;    
            if (x == input) 
            {    
                printf(" correct");       
                printf("Number of Guesses: %d\n", guess);
                exit(0);     
            }    
            //to reset random    
            x = (rand() % 10 + 1);  
            close(fd[0]);   
        } 
    }
    Now that it's in a readable form, it will be more likely for somebody to read it. Nobody would have wanted to bother with the original mess.

    Also, that is not C. It will not compile as C.
    C:TEST>gcc -Wall guess.c
    guess.c: In function `main':
    guess.c:13: warning: implicit declaration of function `fork'
    guess.c:14: parse error before `int'
    guess.c:16: `x' undeclared (first use in this function)
    guess.c:16: (Each undeclared identifier is reported only once
    guess.c:16: for each function it appears in.)
    guess.c:41: parse error before `char'
    guess.c:10: warning: unused variable `i'

    C:TEST>
    If you are using an odd variant like C99, then inform us of that fact. It will save this forum the bandwidth of us having to come back to you to inform you that your program won't even compile.

    And you forgot main's return statement.
    Last edited by dwise1_aol; April 10th, 2013 at 02:04 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by dwise1_aol
    For one thing, format your code! The reason to use code tags is so that your code will not lose its formatting. That jumbled mess that you posted is worse than not using code tags. Whatever demon possessed you to hide all your braces at the ends of lines like that?

    Code:
    #include <stdio.h> 
    #include <unistd.h> 
    #include <time.h> 
    #include <stdlib.h>  
    
    char readbuffer[100]; 
    
    int main() 
    {  
        int pid, nbytes, i, input;   
        int fd[2] = {} ;   
        int guess = 0;   
        pid = fork();  
        int x;   
        srand((unsigned) time(NULL));  
        x = (rand() % 10 + 1);  
        if (pid == 0) 
        {     
            close(fd[0]);    
            printf("enter a number ");     
            scanf("%d", &input); 
            write(fd[1], &input, (sizeof(input)));
            if (x == input) 
            {    
                exit(0);     
            }  
            close(fd[1]);  
        } 
        else 
        {   
            //PARENT     
            close(fd[1]);    
            nbytes = read(fd[0], &input, sizeof(input));  
            while (x != input) 
            {    
                if (x != input) 
                {        
                    guess++;       
                }     
            }   
            char answer;    
            if (x == input) 
            {    
                printf(" correct");       
                printf("Number of Guesses: %d\n", guess);
                exit(0);     
            }    
            //to reset random    
            x = (rand() % 10 + 1);  
            close(fd[0]);   
        } 
    }
    Now that it's in a readable form, it will be more likely for somebody to read it. Nobody would have wanted to bother with the original mess.

    Also, that is not C. It will not compile as C.

    If you are using an odd variant like C99, then inform us of that fact. It will save this forum the bandwidth of us having to come back to you to inform you that your program won't even compile.

    And you forgot main's return statement.
    So sorry its my first time here :
    i use gcc -std=c99 g.c
    and sorry for the indent i was having problems with the tagging and it reformated my code
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    If you are using an odd variant like C99, then inform us of that fact.
    Why do you consider C11 or C99 odd variants. In my opinion these are the current standards and your C90 variant should really be considered the odd variant if any.

    Jim
  8. #5
  9. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    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
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    I call C99 the variant because it is. When C is mentioned, it is C90, not C99. To mention C99 requires that we say "C99". When you pick up a C book, how often is it instead C99? Not very often. If the book is about C99, it will say "C99".

    As far as I can tell, C90 is universally supported in current compilers, but C99 is not. Some current compilers support C99 fully, some only partially, and some not at all nor do they have any plans to support it. Microsoft is the best known example of not supporting C99, because C++ and the STL already do what C99 does so why bother?

    Even though gcc now supports C99 partially, when you run gcc what does it default to? C90, not C99. We both saw that the OP had to tell gcc to not do what it normally would and instead to compile according to the variant standard, C99. And for anyone else to try to compile his code successfully, they would have to either correct it to comply with C90 or else get a compiler that supports C99.

    And just who uses C99? How widespread is its use and support in industry? I've never encountered any programmer in industry, AKA "the real world", who uses C99, nor any company that specifies using C99. Doesn't mean that they don't exist, just that I've never heard of any. So far, the only people I've seen using C99 have been students and hobbyists.

    I'm not saying that nobody should use C99. All I'm saying is that when you do use C99 and want to ask others to compile and test your code, you damned well better tell us up front that you are using something different from what everybody else knows as "C".

    C is C90. C99 is a variant that needs to be identified as C99.

IMN logo majestic logo threadwatch logo seochat tools logo