title: C++: Get numbers between two string delimiters and values after extracting desired integer from string using stringstream
body: <pre><code>    ```
      #include &lt;iostream&gt;
      #include &lt;string&gt;
      #include &lt;sstream&gt;
      #include &lt;vector&gt;
      #include &lt;fstream&gt;
  
      using namespace std;
  
  
      bool findCurrentNumber (int currentNumber, int foundNumber) 
      {
          if (currentNumber == foundNumber) 
          {
              return true;
          }
          return false;
      }
  
  
      int main()
      {
  
          ifstream inputFile;
          inputFile.open("C:\\Projects\\data.txt");
          if (!inputFile) 
          {
              cerr &lt;&lt; "Error reading file "; 
          }
          else 
          {
          int searchCurrentNumber;
          cout &lt;&lt; "Please type in the number you are looking for: ";
          cin &gt;&gt; searchCurrentNumber;
  
          bool foundRelevantSection = false;
          string delimiter = "energy";
          size_t pos = 0;
          string token;
          string line;
  
  
          while (getline(inputFile, line)) 
          {
               while ((pos = line.find(delimiter)) != string::npos) 
               {
                      token = line.substr(0, pos);
                      //check the found token
                      //cout &lt;&lt; token &lt;&lt; endl;
                      line.erase(0, pos + delimiter.length());
                      stringstream ss;     
                      //store the whole string into stringstream
                      ss &lt;&lt; token; 
                      string temp; 
                      int found; 
                      while (!ss.eof()) 
                      { 
                      //extract the word by word from stream
                      ss &gt;&gt; temp; 
                      //Check the given word is integer or not
                      if (stringstream(temp) &gt;&gt; found) 
                      cout &lt;&lt; "Number found: " &lt;&lt; found &lt;&lt; " " &lt;&lt; endl;; 
                      //no space at the end of string
                      temp = ""; 
                      }
                      if (findCurrentNumber (searchCurrentNumber, found) == true)
                      {
                          while (getline (inputFile, line)) 
                          {
                          if (foundRelevantSection) 
                              {
                                  //if no matches were found, the function returns "string::npos"
                                  if(line.find("total") != string::npos) 
                                  {
                                      //relevant section ends now
                                      foundRelevantSection = false;   
                                  }
                                  else    
                                  {
                                  cout &lt;&lt; line &lt;&lt; endl;
                                  }
                              }
                              else 
                              {
                                  if (line.find("point") != string::npos )
                                  {
                                      foundRelevantSection = true;
                                  }
                              }
  
                          }
                      }               
                      else 
                      {
                      cout &lt;&lt; "The number is not equal on this line compared to what is typed in!" &lt;&lt; endl;
                      }
                }                     
          } //closes the while-loop
      } //closes the if/else-statement
  
      inputFile.close();
  
          return 0;
      }
  ```
  </code></pre>
  
  <p>Hi all,</p>
  
  <p>I would like to parse an input file having this format: </p>
  
  <blockquote>
  <pre><code>point     152 # energy  # 0.5
  152 152 152 152 152 152
  152 152 152 152 152 152
  total  0.011  0.049  0.035 
  
  point     153 # energy  # 1.5
  153 153 153 153 153 153
  153 153 153 153 153 153
  total  0.015  0.050  0.040
  </code></pre>
  </blockquote>
  
  <p>The code accepts an user-supplied integer and compares it to the number extracted from e.g. the string "point     152 # energy". If the user enters the number "152", the code should give following numbers: </p>
  
  <blockquote>
  <pre><code>output: 
  152 152 152 152 152 152
  152 152 152 152 152 152
  </code></pre>
  </blockquote>
  
  <p>Unfortunately, my code returns exactly the opposite if number 152 is entered or if number 153 is entered no values are returned. </p>
  
  <p>Could anyone help me and tell me what I am doing wrong? I am grateful for any hint!</p>
  
  <p>Thanks in advance!</p>
  
  <p>Best wishes,</p>
  
  <p>DaveS</p>