• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Having a few issues with a function to test if a number is numeric.

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, first post here but I'm at my wit's end trying to figure out what I've messed up. At a baseline, I'm to make a function to be called upon from a header to test if the contents of a string are numeric (and ultimately able to be converted into a double using another function). For whatever reason, my function is telling me that pretty much everything is non-numeric and I assume it's just a result of some stupid mistake that I'm overlooking. I'd appreciate any help I can get. Apologies for the (presumably) atrocious formatting. The goal is to check for the user possibly having a + or - as the first character of the inputted string (which is okay) and to ensure that all other characters are a digit aside from one allowed decimal.

 
Ranch Hand
Posts: 123
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Hugh

Will have a look at this later if I get time, but can I suggest you try adding a few printfs (or whatever the C++ equivalent is if you're a 'proper' C++ coder, unlike myself)?

For example, I would start by putting one wherever IsNumeric is set false, to identify what the code thinks is wrong, and work back from there. At the risk of stating the obvious, being able to debug your code is a pretty useful (and probably necessary) skill to have.

If you are using an IDE with an integrated debugger then that would also be worth trying - (probably) no need to alter your code then, until you work out what's going wrong anyway

Cheers
John
 
Hugh Chungus
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey John,

Thanks for the incredibly fast reply! I'm basically brand new to this so a fair bit of that is lost on me. Our teacher pretty much just makes us use Dev C++ which is incredibly light on features. I'm going to keep working away on it and see what I can figure out, perhaps something will just click! I'll look into the debugging process in the meantime as well.

Thank you very much for your advice,
Hugh
 
Marshal
Posts: 65114
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please tell us your definition of a number's format. You need to work out the logic/algorithm behind your program before you try to write any code.
 
Hugh Chungus
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell,

In the context of what I've been assigned to work on, the number's format can begin with a + or - (+100 or -200) and can contain a decimal, effectively just a standard double beyond a + being allowed as the first character of the number. Validation for range is going to be handled by a different function that just converts the string that this function deems to be a valid double into an actual double using std::stod. If I misunderstood your question, please let me know, this is all rather new to me.

Thank you for your time,
Hugh
 
Hugh Chungus
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So upon further looking.. I realized that I had one variable that wasn't initialized that was responsible for everything... Thank you for your time, guys. Thanks for making this forum a great place.
 
John Matthews
Ranch Hand
Posts: 123
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good. This could have been caught by following my earlier suggestion, adding something like this before line 56:
I appreciate this might not mean much to you yet, but being able to examine values in this simplistic way is invaluable. Definitely worth spending a bit of time finding out about printf() (C) and/or cout (C++).
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!