diff options
-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; } |