Jfunch 0 Newbie Poster

The problem I am trying to solve is

Write a program using fork() to compute average of N numbers. The parent process forks a child process that takes input from the user, adds all the numbers and returns the sum and the count of numbers to the parent process that divides the sum by the count of the number, and prints out the average.

right now I just have a program where the user enters in a number in the child process and the parent process prints it out just to test things out. The problem I am having is I'm not sure how to send the data to the parent process from the child. If someone could help me out I would appreciate it. Also if I'm going at it with the wrong approach, I would appreciate suggestions as well. My code so far is below.

#include <iostream>
#include <string>

// Required by for routine
#include <sys/types.h>
#include <unistd.h>

#include <stdlib.h>   // Declaration for exit()

using namespace std;

   string sIdentifier;

   pid_t pID = fork();
   if (pID == 0)                // child
      sIdentifier = "Child Process:";
      int num;
      // Code only executed by child process
      cout << "Enter a num" << endl;
      cin >> num;

    else if (pID < 0)            // failed to fork
        cerr << "Failed to fork" << endl;
        // Throw exception
    else                                   // parent

      // Code only executed by parent process

      sIdentifier = "Parent Process:";
      cout << num;

    // Code executed by both parent and child.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.