So I'm reading in user input that has the form 1U = 1.2A the only variation is that A will be a different letter from B to E. I will be receiving 7 such inputs. One for each letter A through E. I need to extract the floating point number next to the A. So in the previous example I trying to get 1.2. I do this by putting the user input into an istringstream variable and extrafting what I need from there

#include <iostream>
#include <sstream>
using namespace std;

string rateInfo;
istringstream inSS;
double rate[7], temp;

for (int i = 0; i < 7; i++) {
    getline(cin, rateInfo);
    inSS >> temp;
    rate[i] = temp;

This code actually mostly works. Except for when i = 4. The specific input I'm testing when i = 4 is 1U = 5.6E. Instead of reading in 5.6 it reads in 0.

I also discovered if input 1U = 5.6 instead of 1U = 5.6E my code works perfectly. I've also tested different numbers along with E such as 6.54 and 1.23 and still everything works except for the E case. So it seems something about the letter E is breaking my code.

I know that the extraction operator reading in 0 is one of the things that can happen when the stream is in an error state but for the life of me I can't figure out exactly what is wrong. Especially since it works for every letter except E.

0 Answers