diff options
author | tomsmeding <tom.smeding@gmail.com> | 2017-05-17 20:36:55 +0200 |
---|---|---|
committer | tomsmeding <tom.smeding@gmail.com> | 2017-05-17 20:36:55 +0200 |
commit | b7095bb0f5f9d966ffeb62ce37cd2820facc1a8e (patch) | |
tree | de98a98d3c3dc946a797b3a83d88596592ed8928 | |
parent | d71b1fe6d65e72f9a4f3f81ffe06124d1f60458c (diff) |
Improve parsing of invalid floats
-rw-r--r-- | stddev.cpp | 16 |
1 files changed, 15 insertions, 1 deletions
@@ -1,6 +1,7 @@ #include <iostream> #include <vector> #include <cmath> +#include <cctype> using namespace std; @@ -17,11 +18,24 @@ int main(int,char **argv){ vector<double> data; + bool warned=false; + double total=0; + string word; while(true){ + cin>>word; double v; - cin>>v; if(!cin)break; + const char *start=word.c_str(); + char *endp; + v=strtod(start,&endp); + if(endp!=start+word.size()||!isfinite(v)){ + if(!warned){ + warned=true; + cerr<<"Warning: invalid floating-point values detected"<<endl; + } + continue; + } data.push_back(v); total+=v; } |