title: How to make shell continually ask for input after execv in C++
body: <p>I'm attempting to implement a shell in c++, using <code>fork()</code> and <code>exec()</code>. My code is as follows:</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sstream&gt;
  #include &lt;vector&gt;
  
  using namespace std;
  
  int main(int argc, char** argv){
      while(true){
          cout &lt;&lt; &quot;minish&gt; &quot;;
          string input = &quot;&quot;;
          getline(cin, input);
  
          int count = 1;
          int shouldCount = 1;
          if(input.length() == 0) shouldCount = 0;
  
          if(shouldCount == 1){
              int didFindLetter = 0;
              for(int i = 1; i &lt; input.length()-1;i++){
                  if(input[i] ==' ' &amp;&amp; didFindLetter == 1){
                      count++;
                      didFindLetter = 0;
                  }
                  else if(input[i]!=' '){
                      didFindLetter = 1;
                  }
              }
          }
      
          //need to create a wordArray here...
          vector&lt;string&gt; wordArray;
          std::istringstream iss (input);
          string tempWord;
          for(int i = 0; i &lt; count; i++){
              iss &gt;&gt; tempWord;
              wordArray.push_back(tempWord);
          }
          
          char* argsArray[1024];
          count = 0;
          for (int i = 0; i &lt; wordArray.size(); i++) {
              //strdup returns pointer to a char array copy of its parameter
              argsArray[count++] = strdup(wordArray[i].c_str());
          }
          argsArray[count++] = (char *)NULL;
  
  
          pid_t pid = fork();
  
          if(pid == 0){
              //child
              execvp(argsArray[0], argsArray);
              fprintf(stderr, &quot;exec: %s\n&quot;, strerror(errno));
              exit(errno);
  
          }
          else if(pid == 1){
              //int waitStatus;
              //pid_t terminated_child_pid = wait(&amp;waitStatus);
              wait(NULL);
          }
      }
  
      return 0;
  }
  </code></pre>
  <p>When I run this code and try to execute a single command, it seems to work properly</p>
  <blockquote>
  <p>Elliots-MacBook-Pro:minish Elliot$ ./minish</p>
  <p>minish&gt; ls</p>
  <p>minish&gt; makefile  minish      minish.cpp  test</p>
  </blockquote>
  <p>After running ls via execv, the code does not print &quot;minish&gt; &quot; to prompt me for more input, however, is able to continually execute if i continue to type commands i.e &quot;ls&quot;.</p>
  <p>What is the cause of this problem, and how might i be able to fix it?</p>