Note that there are some explanatory texts on larger screens.

plurals
  1. POreading piped stdin and user input
    primarykey
    data
    text
    <p>I'm writing a program for a c++ data structures class that reads in a space separated file and I wrote a small function so that I could pipe in different files and work with them, but I would also like to take user input using cin and it seems that the buffer is just looping. I'm sort of beyond my depth here, but here's my input function. I'm running the program by $ cat filename | ./compiledexec. I was hoping someone might know why using cin in other places doesn't wait for user input and perhaps help with a solution?</p> <pre><code> void catchPipe(int dataArray[][9]); int main(){ int inArray[9][9]; int column; catchPipe(inArray); cout &lt;&lt; "Which column would you like to check?"; cin &gt;&gt; column; // This input is skipped totally. functionChecksIfInCol(column); //Function called with garbage value cout &lt;&lt; "end program" &lt;&lt; endl; return 0; } void catchPipe(int dataArray[][9]){ int i; int n=0; int pos=0; string mystring; while(cin){ getline(cin, mystring); if( n &lt; 9 ){ for(i = 0; i &lt; mystring.length(); i++){ if( (int)mystring[i] != 32 ){ dataArray[n][pos] = mystring[i] - '0'; pos++; } }pos =0; ++n; } } }// end catchPipe() //Sample File input: 0 8 0 1 7 0 0 0 3 0 2 0 0 0 0 0 0 9 0 9 0 0 3 0 5 4 8 0 0 4 0 9 0 0 0 0 0 0 0 7 0 3 0 0 0 0 0 0 0 1 0 4 0 0 6 1 9 0 8 0 0 5 0 7 0 0 0 0 0 0 8 0 2 0 0 0 6 4 0 1 0 </code></pre> <p>Thanks!</p> <p>The program fills in my inArray, but it skips the next call to cin. I'm assuming this is because stdin has been redirected from the keyboard to the pipe from linux? Maybe I can declare another istream object and direct it to the keyboard or something? I'm not sure what to do here</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload