C Simple child and parent back and forth
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: C Simple child and parent back and forth

Hybrid View

  1. #1
    Join Date
    Apr 2013
    Posts
    1

    C Simple child and parent back and forth

    'm 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
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,381

    Re: C Simple child and parent back and forth

    I can't get it to work fully.
    So what does work and what doesn't? You haven't explained the problem. What debugging of the code have you done? Have you stepped through the code in the debugger to see where is the problem?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,381

    Re: C Simple child and parent back and forth

    Taking a quick look, what do expect from this code?

    Code:
    while (x != input) {
          if (x != input) {
               guess++;
          }
    }
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center