summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2017-05-17 20:36:55 +0200
committertomsmeding <tom.smeding@gmail.com>2017-05-17 20:36:55 +0200
commitb7095bb0f5f9d966ffeb62ce37cd2820facc1a8e (patch)
treede98a98d3c3dc946a797b3a83d88596592ed8928
parentd71b1fe6d65e72f9a4f3f81ffe06124d1f60458c (diff)
Improve parsing of invalid floats
-rw-r--r--stddev.cpp16
1 files changed, 15 insertions, 1 deletions
diff --git a/stddev.cpp b/stddev.cpp
index ee36af4..5920b6d 100644
--- a/stddev.cpp
+++ b/stddev.cpp
@@ -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;
}