CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  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
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

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
  •  





Click Here to Expand Forum to Full Width

Featured