summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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;
}