Troubles with pipes, fork, and dup2

I want to execute metasploit by two pipes to communicate with it, but I have troubles with that communication. When I run my program, I get this error: "stty: standard input: Inappropriate ioctl for device" and I don't receive the metasploit promt.
just select an exploit.
This is my code:

#include <stdio.h>
 #include <stdlib.h>
 #include <string.h> 
#include <errno.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <fcntl.h> 
#include <unistd.h> 
#include <iostream> 

 int main(int argc, char** argv) { 

     int pipeIn[2];    
     int pipeOut[2];     
     pipe(pipeIn);
     pipe(pipeOut);
     pid_t hijo=fork();

     if (hijo==0) {
         dup2(pipeIn[0], STDIN_FILENO);
         dup2(pipeOut[1], STDOUT_FILENO);
         dup2(pipeOut[1], STDERR_FILENO); 
         close(pipeIn[0]);         close(pipeOut[1]);
          close(pipeIn[1]);         close(pipeOut[0]); 

        char* argv[]={"msfconsole", NULL};
         execvp("msfconsole", argv);

     }else if (hijo==-1) { 
        perror("fork");         exit(EXIT_FAILURE);     

} else{//padre  
       close(pipeIn[0]);         close(pipeOut[1]); 

        char buf[1024];
         int count;
           bool b=true;
         int ccount=0;
          for (;b;){
             while (b && (count=read(pipeOut[0], buf, sizeof(char) * 1024))>0){
                 buf[count]='\0'; 
                 printf ("%s", buf);
                 if (strstr(buf, "msf >")!= 0){
                      write(pipeIn[1], "use exploit/windows/fileformat/adobe_pdf_embedded_exe_nojs \n\0 ", sizeof(char)*1024);
                     if (ccount++>=1){ printf ("encontrado"); b=false; }
             } }
          }
               close(pipeIn[1]); 
              close(pipeOut[0]);
           }}


                 It shows this:
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMM                MMMMMMMMMM
MMMN$                           vMMMM
MMMNl  MMMMM             MMMMM  JMMMM
MMMNl  MMMMMMMN       NMMMMMMM  JMMMM
MMMNl  MMMMMMMMMNmmmNMMMMMMMMM  JMMMM
MMMNI  MMMMMMMMMMMMMMMMMMMMMMM  jMMMM
MMMNI  MMMMMMMMMMMMMMMMMMMMMMM  jMMMM
MMMNI  MMMMM   MMMMMMM   MMMMM  jMMMM
MMMNI  MMMMM   MMMMMMM   MMMMM  jMMMM
MMMNI  MMMNM   MMMMMMM   MMMMM  jMMMM
MMMNI  WMMMM   MMMMMMM   MMMM#  JMMMM
MMMMR  ?MMNM             MMMMM .dMMMM
MMMMNm `?MMM             MMMM` dMMMMM
MMMMMMN  ?MM             MM?  NMMMMMN
MMMMMMMMNe                 JMMMMMNMMM
MMMMMMMMMMNm,            eMMMMMNMMNMM
MMMMNNMNMMMMMNx        MMMMMMNMMNMMNM
MMMMMMMMNMMNMMMMm+..+MMNMMNMNMMNMMNMM
        http://metasploit.pro


       =[ metasploit v4.7.2-1 [core:4.7 api:1.0]
+ -- --=[ 1211 exploits - 733 auxiliary - 202 post
+ -- --=[ 317 payloads - 30 encoders - 8 nops

stty: standard input : Inappropriate ioctl for device
stty: standard input : Inappropriate ioctl for device
stty: standard input : Inappropriate ioctl for device
stty: standard input : Inappropriate ioctl for device
stty: standard input : Inappropriate ioctl for device
stty: standard input : Inappropriate ioctl for device
stty: standard input : Inappropriate ioctl for device
^C

Looks like another hacker tool masquerading as "Penetration Testing Software".

I can't really see legit reason to automate running this unix through pipes.

Firstly, thank you for your answer. But I have one question: what would you do to automate it?