Like Tree2Likes
  • 1 Post By Salem
  • 1 Post By AndiPersti

C Simple child and parent back and forth

This is a discussion on C Simple child and parent back and forth within the C Programming forums, part of the General Programming Boards category; Im creating a small guess game between a child and a parent: The child takes in the input from user ...

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    3

    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]);   } }
    Last edited by LivingLegend; 04-10-2013 at 12:49 PM. Reason: indent

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,443
    SourceForge.net: Indentation - cpwiki
    Nobody is going to try and pick apart code which looks like it was indented by a dog chasing a stick.
    Matticus likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    3
    srry my code was well indented but for some reason the code tag messed it one sec let me fix it didnt realize it

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,443
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    int fd[2] = {} ;   
    ...
    close(fd[0]);    
    ...
    write(fd[1], &input, (sizeof(input)));
    Well, it looks like you want to use a pipe but you never create one. Just declaring an array fd isn't enough.

    Code:
      
    nbytes = read(fd[0], &input, sizeof(input));
    You capture the return value of read() which is good, but you never use it. I can tell you that its value is -1 which means there was an error. The reason for the error is explained above.

    Code:
        while (x != input)
        {    
          if (x != input) 
          {        
             guess++;       
          }     
        }
    Typical infinite loop because you neither change "x" nor "input" within the loop.

    Code:
     //to reset random   
     
        x = (rand() % 10 + 1);
    ???

    Generally, crossposting is considered bad form.

    Bye, Andreas
    Salem likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IPC communication between parent and child
    By Durango2011 in forum Linux Programming
    Replies: 1
    Last Post: 10-23-2012, 04:36 AM
  2. parent child process
    By simo_mon in forum C Programming
    Replies: 2
    Last Post: 08-03-2008, 03:44 AM
  3. calling parent from child
    By Aemon07 in forum Windows Programming
    Replies: 5
    Last Post: 07-25-2007, 09:27 PM
  4. C# parent child
    By BraneMxm in forum C# Programming
    Replies: 2
    Last Post: 06-02-2007, 10:24 AM
  5. Creating Child with parent of Child
    By SilkySmooth in forum Windows Programming
    Replies: 3
    Last Post: 06-28-2003, 12:15 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21