title: Using stringstream.fail() to check whether input is int or string
body: <p>I'm creating a simple function printNode that takes in an int or the string "all", and prints a corresponding output. The int aspect of the function works well, but when I call</p>
  
  <pre><code>printNode all
  </code></pre>
  
  <p>the function prints "Error: invalid argument", and I can't seem to figure out why. </p>
  
  <pre><code>int main() {
      ...
      getline(cin, line)
      stringstream lineStream(line);
      stringstream elineStream(line);
      printNode(lineStream, elineStream);
      ...
  }
  
  
  int printNode(stringstream&amp; lineStream, stringstream&amp; elineStream) {
  //String aspect of printNode
  int nodeid;
  string name;
  
  // Parsing nodeid or name
  lineStream &gt;&gt; nodeid;
  
  // If parsing to nodeid fails, check if input is string "all"
  if (lineStream.fail()) {
      elineStream &gt;&gt; name;
      if (elineStream.fail()) {
          if (elineStream.eof()) {
              cout &lt;&lt; "Error: too few arguments" &lt;&lt; endl;
              return 0;
          }
          else {
              cout &lt;&lt; "Error: invalid argument" &lt;&lt; endl;
              return 0;
          }
      }
      if (name != "all") {
          cout &lt;&lt; "Error: invalid argument" &lt;&lt; endl;
          return 0;
      }
      // Check for too many arguments
      elineStream &gt;&gt; ws; 
      if (!elineStream.eof()) {
          cout &lt;&lt; "Error: too many arguments" &lt;&lt; endl; 
          return 0;
      }
  
      // Print output for input "all" 
      cout &lt;&lt; "Print: all nodes" &lt;&lt; endl;
      return 1;
  }
  </code></pre>
  
  <p>Any help would be much appreciated. </p>