cihanyyt / elements-of-programming-interviews

Automatically exported from code.google.com/p/elements-of-programming-interviews
Other
0 stars 0 forks source link

stringToInt in problem 5.6 accepts invalid input "-" #14

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Problem 5.6 specifies that stringToInt should throw an exception if the input 
string does not encode an integer.  "-" does not seem to encode an integer, 
though the stringToInt in Interconverting_string_integer.cpp returns 0 in this 
case.  While a somewhat minor problem, I think it's good to be precise.

I think it might be better for this problem to define explicitly the set of 
strings that have integer representations (e.g. I would choose those matching 
regexp -?[0-9]+ , allowing leading zeros and prohibiting "-").

Patch tested by copying out stringToInt function and using in separate file, 
since main file does not compile with g++ version 4:4.6.3-1ubuntu5 using 
command line "g++ -o int_string Interconverting_string_integer.cpp".

Original issue reported on code.google.com by amd...@gmail.com on 4 Sep 2013 at 2:44

Attachments:

GoogleCodeExporter commented 8 years ago
Hey Alan,

Thanks for your report, and it is really a good catch since this problem has 
been here for a long time, and you are the first one to catch this!

Original comment by TsungHsi...@gmail.com on 4 Sep 2013 at 5:12