diff options
author | tomsmeding <tom.smeding@gmail.com> | 2017-02-05 11:07:05 +0100 |
---|---|---|
committer | tomsmeding <tom.smeding@gmail.com> | 2017-02-05 11:07:27 +0100 |
commit | 7144d80e4f6e90bf82986c48492e3cf0a8072dfa (patch) | |
tree | 2a695f5e1def4ad21483eda4f4ed41ba6c9a80ed | |
parent | 46b2b554669c597318a0a5ae41043e2e9c912e18 (diff) |
Better error checking (avoid a div-by-0)
-rw-r--r-- | histogram.cpp | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/histogram.cpp b/histogram.cpp index a12cd75..2a275e7 100644 --- a/histogram.cpp +++ b/histogram.cpp @@ -68,7 +68,13 @@ int main(int argc,char **argv){ } if(nbins<1){ - cerr<<"Invalid number of bins '"<<nbins<<"'"<<endl; + cerr<<"histogram: Invalid number of bins '"<<nbins<<"'"<<endl; + usage(); + return 1; + } + + if(havebounds&&high<=low){ + cerr<<"histogram: high <= low"<<endl; usage(); return 1; } @@ -112,6 +118,11 @@ int main(int argc,char **argv){ histogram[binidx]=tally; if(tally>maxtally)maxtally=tally; + if(maxtally==0){ + cerr<<"histogram: No data in range"<<endl; + return 1; + } + char fullbar[BARWIDTH+1]; memset(fullbar,'#',BARWIDTH); fullbar[BARWIDTH]='\0'; |