input errors c++

I'm a new member but a long time lurker, this forum is really great for just about everything.

I seem to be having a problem with my code for c++ using xcode.

Basically, the searchfunction returns a value of i which determines the location of the customer record in the stuct of arrays.

If the function finds the person, it will return a value of >= 0. Otherwise, it will return -1. I believe that works as intended, but what doesnt work is when I prompt the user for a deposit amount. Everything works fine, until a user inputs a letter or string.

It's the same horrible behavior that scanf() has, crap gets stuck in the buffer, enter infinite loop. The most robust way to validate input is to read into a string, then validate and convert to a numeric type.

How nice then that STL have a string class, but unfortunately it doesn't include a numeric conversion function. So use boost then, or go back and forth between string and c_string and use standard C library functions to convert to numeric types. It's not pretty.

strtof is rather nice, but it reads up until the first non-numeric character so 99kk would return 99. It updates it's end pointer argument though, so you can compare it to the start pointer + strlen() and if they don't match, reject.

Looking at you first code example this might become messy, so create your own input validation function or subclass existing STL classes to do what you want and test separately. Convert the number and if the input is rejected throw an exception. You can then validate all the conversion inside a try/catch block.

What I mean is, I just want to do error check regardless if the user enters a letter or numbers followed by a letter.

@subsonix
I'm a bit confused, I think what your trying to have me do is a bit too advance, are you saying to create a function that checks the input of amount?

So I create a function with a pass by value of amount? How will I be able to convert a string to numeric?

All I want is an error check, so I thought either !cin or cin.fail() would work.

Im getting pretty desperate and don't know what else to try.

Click to expand...

Well from your comment "I think what your trying to have me do is a bit too advance"d I'm now thinking this might be homework. If it is the following possible solution will qualify as to advanced and you won't be able to turn it in. If not I think it will work although I haven't had the chance to test it much.

Yep, a little too advance lol, is there anything simpler? This is for CS1 final, just trying to cover all my bases for what the professor will input and I'm sure he'll enter a letter to check for input errors.

I'm using !cin for anything that goes into amount and when I debug it, the value of amount stays at zero. Even though I put 2p, urgh this error checking is getting really complicated

The problem is what subsonix said: There is stuff in the input buffer which is not numerical. When you use the >> operator to grab a number from the input stream, it will consume data up to the first non-numerical character. Even if that means not consuming anything at all, because the first character is already not numerical. Checking cin.fail() won't help, because this is not considered an error: The input stream is fine.

Parsing the numbers yourself (like in lloyddean's post) is an excellent exercise, but the link I posted above contains alternatives.

The problem is what subsonix said: There is stuff in the input buffer which is not numerical. When you use the >> operator to grab a number from the input stream, it will consume data up to the first non-numerical character. Even if that means not consuming anything at all, because the first character is already not numerical. Checking cin.fail() won't help, because this is not considered an error: The input stream is fine.

Parsing the numbers yourself (like in lloyddean's post) is an excellent exercise, but the link I posted above contains alternatives.

Click to expand...

You know what, I saw that a couple of days ago, but it was @ 4am and I forgot to bookmark it. It seems like a better way. Thank you!

I would try today, but I only have my iPad with me. Plus I have a clearsscreen function that is all over my code that Xcode doesn't like since it uses the windows.h header files.

I'm a bit confused, I think what your trying to have me do is a bit too advance, are you saying to create a function that checks the input of amount?

So I create a function with a pass by value of amount? How will I be able to convert a string to numeric?

All I want is an error check, so I thought either !cin or cin.fail() would work.

Im getting pretty desperate and don't know what else to try.

Click to expand...

The code snippet I posted does what you asked for, it reads in a string, then check to see if all characters where converted to a number. If not, then there where some non-numeric characters in there. You could use this as it is. My comment about creating a function or subclass STL string to add your own .to_string() function was only an idea of how you could refactor you code and separate input validation from your handling of bank accounts.

Here is an idea of how you could subclass STL string. You create a numeric_string class that inherits from STL string. It does exactly what I did in my previous post but throws an exception if it did not manage to convert all characters in the string. (strtof can also detect overflows btw, so you could potentially check for, and throw and exception for that as well)

Further down we would now use our new numeric_string class, create an object called input and handle all inputs inside a try block. If we get the the bottom of all input, we set a 'valid_input' bool to true, if to_float() fails, it will throw an exception and we will end up in the catch block, an error messege is logged to stderr and 'valid_input' is set to false. Finally, we check if 'valid_input' is false in the loop condition. In this example I just read in three float variables, width, height, depth.

@subsonic. -- wow I would love to try your approarch to this, but sadly not enough time to finish before tomorrow. I have some other finals to take care of and we didn't really get to objects much, we barely touched classes.

I would though try it out this weekend and see how I get it to work. Thanks =)

Neither accept leading or trailing spaces and it is questionable if they should.

Only one will handle trailing non-numerics or mutliple decimal points corrrectly.

Click to expand...

Here, I get the first version to ignore leading/trailing space. While the second version interpret it as part of the number. IMO, it's more natural to ignore it as in the first version. To me that seems like the most natural way to handle input, for a promt at least.

@llyodean, that code is a bit too advance for what I'm doing. I can barely understand the code (knowledge isn't all there yet). Thank you I will try this out as well this weekend.

I'm probably going to create a function that checks my amount. I will pass the value of amount into the function that sanders posted through the link. And hopefully, it will return a value where my while statement will process.

Well, I went ahead and followed what Sanders had recommended and while it does check for letter input, it doesnt check for anything else like 5.2R instead of 5.24, typos is going to hurt but hey atleast it checks for letters lol.

I just call it into amount and so far it works for what its intended. I dont think I'm going to have time to check for trailing mistakes.

MacRumors attracts a broad audience
of both consumers and professionals interested in
the latest technologies and products. We also boast an active community focused on
purchasing decisions and technical aspects of the iPhone, iPod, iPad, and Mac platforms.