title: Removing non-integers from a string in C++
body: <p>There was a passing comment in a book of mine about people inputting commas into integers and messing up your program, but it didn't elaborate. That got me thinking, so I tried writing a little algorithm to take an std::string and remove all non-integer characters. This code compiles but skips over output. Why isn't anything being assigned to newstring? Does if(isdigit(fstring[i])) evaluate to true of the address it's pointing to hold a digit? </p>
  
  <pre><code>//little algorithm to take the non-integers out of a string
  //no idea on efficiency
  
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  
  int main()
  {
      std::cout &lt;&lt; "Enter a number with non-integer characters: ";
  
      std::string fstring; 
      getline(std::cin, fstring);
  
      std::string newstring;
  
      int i = 0, x = 0;
  
      while (i != fstring.length())
      {
          if (isdigit(fstring[i]))
          {
              newstring[x] = fstring[i];
              i++;
              x++;
          }
          else
          {
             i++;
          }
      }
  
      std::cout &lt;&lt; std::endl;
      std::cout &lt;&lt; newstring;
      system("PAUSE");
  }
  </code></pre>
  
  <p>Secondary question, that perhaps belongs elsewhere: how do you convert a string to an int (or a floating point number)?</p>