From dd3bd1224505413bb91a3f99a95a3936e1347f21 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Tue, 6 Oct 2015 18:44:48 -0700 Subject: 2255 - bug in detecting non-integers Thanks Caleb Couch. --- 014literal_noninteger.cc | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/014literal_noninteger.cc b/014literal_noninteger.cc index 11a3fc70..643ad587 100644 --- a/014literal_noninteger.cc +++ b/014literal_noninteger.cc @@ -20,7 +20,7 @@ if (is_noninteger(s)) { :(code) bool is_noninteger(const string& s) { return s.find_first_not_of("0123456789-.") == string::npos - && s.find('.') != string::npos; + && std::count(s.begin(), s.end(), '.') == 1; } double to_double(string n) { @@ -30,3 +30,10 @@ double to_double(string n) { assert(*end == '\0'); return result; } + +void test_is_noninteger() { + CHECK(!is_noninteger("1234")); + CHECK(!is_noninteger("1a2")); + CHECK(is_noninteger("234.0")); + CHECK(!is_noninteger("...")); +} -- cgit 1.4.1-2-gfad0