Thread: User input
View Single Post
Old 04-19-2005, 06:07 PM   #5 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Quote:
After first running some tests, I realized if you typed a space first, you tricked the entire if statement, as it was only looking for "-" or whether it was a digit. To fix this,...
Fix? This function never intended to "fix" user input. This function wants "strictly" a valid integer. Even a "space-typo" is considered an error and therefore the function didn't process it. Besides, your refactor isn't correct:
what if I enter " -5"? Notice the space before the "-"!

Quote:
I also took cstdlib, and took out size_t for an int. Althoguh I got a warning from the compiler, no errors arised, although I am sure there was a reason for the size_t declaration.

If I may ask, what is a size_t variable? I cant seem to find a definition with google, or some other code sites.
So you replaced definitions yet you don't know what you have replaced...
I'll explain you what that is.

std::size_t is a type that will be converted to the proper unsigned int of your system. The normal int versions differ per unique system: it may be a 16 bit system, 32 bit system, 64 bit system and many more types exist. Think of embedded software for robots.
std::size_t will covert the "unsigned int" into the proper version, making the program only more portable.
So in one sentence:
std::size_t will abstract platform dependent unsigned integer sizes for maximum compatibility.

std::size_t will always be a positive number, which is exactly what we want because indexes can't be negative. All you need to make sure that your array doesn't have 2^31 elements (for a 32-bit system).

Quote:
As a final piece, I tried to remove the need for the sstream lib, using indirection to get the contents of the string into an int variable, but it didnt work.
Because you don't understand the code fully. Google to learn the ways of <sstream>, istringstream and ostringstream.
Once you did that and understand it well, you'll be able to build a function far more efficient then this one. But I left this one as it is so people can play with it. There is a lot to improve.

But you did remove:
Code:
  bool bNonDigit = false;
  bNonDigit = false;
which is good. That was a leftover from my personal library when I modified it a bit.

At least you're thinking. That I appreciate. Study the three things I just told you about, and then let's see what we can do from here .
__________________

Last edited by Valmont; 04-19-2005 at 06:40 PM.
Valmont is offline   Reply With Quote