C++ Weird istringstream behaviour
<p>I apologise for being a C++ novice. I am trying to learn by translating some code from Java.</p>

<p>In this code, the strings that are read in represent letter frequencies in % in a language.</p>

<p>The test string we are dealing with is <code>a10b10c10d10e10f50</code>, as the test code shows.</p>

<p>In the loop below, the first 3 values (0.1, 0,1, 0.1) are correctly read in, as are the 5th and 6th (0.1 and 0.5), but for some reason the 4th value for letter 'd', when <code>i</code> is 9, is read in as <code>1000000000.0000000</code>, according to the debugger in Visual Studio. Any ideas why?</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;sstream&gt;
#include &lt;string&gt;
#include &lt;cmath&gt;

using namespace std;

class SymbolFrequency {
public:
    double language(vector &lt;string&gt; frequencies, vector &lt;string&gt; text) {

        for (unsigned int fset = 0; fset &lt; frequencies.size(); fset++) {
            double expected[26];
            fill(expected, expected + 26, 0.0);
            for(unsigned int i = 0; i &lt; frequencies[fset].length(); i += 3) {
                char letter = frequencies[fset][i];
                istringstream is(frequencies[fset].substr(i + 1, i + 3));
                double freq;
                is &gt;&gt; freq; freq /= 100;
                expected[letter - 'a'] = freq;
            }
...
</code></pre>

<p>Test code:</p>

<pre><code>int main() {
    SymbolFrequency s;
    const char *fargs[] = {"a10b10c10d10e10f50"};
    vector&lt;string&gt; f(fargs, fargs + 1);
    vector&lt;string&gt; t;
    t.push_back("abcde g");
    double d = s.language(f,t);
    cout &lt;&lt; (d) &lt;&lt; endl;
    cin.get();
    return 0;
}
</code></pre>
