Computer Science Canada Crash-Proofing.. Need help :) |
Author: | LaZ3R [ Tue Mar 04, 2008 6:49 pm ] |
Post subject: | Crash-Proofing.. Need help :) |
So our teacher wants our next assignment to be crash proof (also known as idiot proof). In short, if the keyboard is thrown at the wall, no matter what sequence of keys are hit, the program will not crash ![]() SO, getting to my problem: I have float varibles declared for a given mass, length, width, height, volume. If the user enters "asndgkjasngdkjsg"and the program attempts to perform a calculation with it, the whole program will essentially screw up. I understand how to make it so that if a user enters a string input, then I know how to limit the only options the program will accept. I'm not sure how to do this however mainly because I'm not even sure how to get C++ to figure out what the length of the string entered into a float variable is, making matters worse ![]() Any help or point in the right direction as to what variables I may need or libraries would be nice ![]() |
Author: | HeavenAgain [ Tue Mar 04, 2008 6:54 pm ] |
Post subject: | RE:Crash-Proofing.. Need help :) |
exceptions handling |
Author: | nike52 [ Tue Mar 04, 2008 7:51 pm ] |
Post subject: | Re: Crash-Proofing.. Need help :) |
LaZ3R @ Tue Mar 04, 2008 6:49 pm wrote: figure out what the length of the string entered into a float variable is, making matters worse
![]() Any help or point in the right direction as to what variables I may need or libraries would be nice ![]() http://cplusplus.com/reference/string/string/size.html Make it a string first and then use str.size(). |
Author: | OneOffDriveByPoster [ Tue Mar 04, 2008 7:57 pm ] | ||
Post subject: | Re: Crash-Proofing.. Need help :) | ||
Write your own version of the following (because it is messier than necessary):
|
Author: | Saad [ Tue Mar 04, 2008 8:02 pm ] | ||||
Post subject: | RE:Crash-Proofing.. Need help :) | ||||
Is it me or do I see a memory leak at
|
Author: | md [ Tue Mar 04, 2008 8:21 pm ] | ||
Post subject: | RE:Crash-Proofing.. Need help :) | ||
From http://www.augustcouncil.com/~tgibson/tutorial/iotips.html
Basically it reads a float; and if there is an error it ignores that line of text and tries to read another float. It will continue to try until it gets valid input. And Saad is correct, OneOffDriveByPoster's code has a nice bug fat memory leak in it. |
Author: | OneOffDriveByPoster [ Tue Mar 04, 2008 8:29 pm ] |
Post subject: | Re: RE:Crash-Proofing.. Need help :) |
Quote: And Saad is correct, OneOffDriveByPoster's code has a nice bug fat memory leak in it. Show me. |
Author: | Saad [ Tue Mar 04, 2008 8:40 pm ] | ||
Post subject: | Re: RE:Crash-Proofing.. Need help :) | ||
OneOffDriveByPoster @ Tue Mar 04, 2008 8:29 pm wrote: Quote: And Saad is correct, OneOffDriveByPoster's code has a nice bug fat memory leak in it. Show me.
Where is this memory freed? |
Author: | OneOffDriveByPoster [ Tue Mar 04, 2008 8:42 pm ] | ||
Post subject: | Re: RE:Crash-Proofing.. Need help :) | ||
Saad @ Tue Mar 04, 2008 8:40 pm wrote: OneOffDriveByPoster @ Tue Mar 04, 2008 8:29 pm wrote: Quote: And Saad is correct, OneOffDriveByPoster's code has a nice bug fat memory leak in it. Show me.
Where is this memory freed? The memory was not dynamically allocated. |
Author: | md [ Tue Mar 04, 2008 9:11 pm ] | ||||||
Post subject: | Re: RE:Crash-Proofing.. Need help :) | ||||||
OneOffDriveByPoster @ 2008-03-04, 8:29 pm wrote: Quote: And Saad is correct, OneOffDriveByPoster's code has a nice bug fat memory leak in it. Show me.Let's start by making a test file:
Compiling this gives the following errors:
Now... ignoring the fact that your code simply doesn't work, you are bastardizing a while loop in order to make it as short as possible. Exceptionally poor form. Let's re-write your code in many lines, which is as best as I can make out
Now... you call the destructor of ss (very bad! you should never be calling destructors yourself!); and then you create a new stringstream (which is indeed dynamically allocated; that's what new does). Then you get the number out of the string stream... which has the exact same issues as getting a number from cin. All in all some horrible code that doesn't seem to solve the problem and uses practices that, were I your boss, would strongly consider firing you over (incompetence of that level cannot be corrected). Clearly I may have miss-interpreted how your code is supposed to work; and possibly there is some minor bug which is what's causing hte compile error. But that new isn't going away; and without a call to delete you are most definitely allocating memory dynamically and then forgetting about it. |
Author: | OneOffDriveByPoster [ Tue Mar 04, 2008 9:13 pm ] | ||
Post subject: | Re: RE:Crash-Proofing.. Need help :) | ||
md @ Tue Mar 04, 2008 9:11 pm wrote:
|
Author: | md [ Tue Mar 04, 2008 9:14 pm ] |
Post subject: | RE:Crash-Proofing.. Need help :) |
I copied and pasted; can you re-write it on multiple lines so I can more easily point out the flaws? ![]() Also... appologies for the infinite posts. |
Author: | OneOffDriveByPoster [ Tue Mar 04, 2008 9:18 pm ] | ||
Post subject: | Re: RE:Crash-Proofing.. Need help :) | ||
md @ Tue Mar 04, 2008 9:14 pm wrote: I copied and pasted; can you re-write it on multiple lines so I can more easily point out the flaws? That's fine. I'll edit this post as I put it together. For now the key is to "google" lib.new.delete.placement
![]() Also... appologies for the infinite posts. New version remains somewhat obfuscated, but I believe that is necessary to enable comparison between the different versions.
Post is done. |
Author: | Saad [ Tue Mar 04, 2008 9:42 pm ] |
Post subject: | RE:Crash-Proofing.. Need help :) |
Ah, the link helped me remember about allocating memory at a specific address. It seems you were correct. I was not accustomed to not seeing delete after new which is why I said it so my apologies. |
Author: | md [ Tue Mar 04, 2008 9:43 pm ] |
Post subject: | RE:Crash-Proofing.. Need help :) |
Ahh, I did not know you could use new in that way. Mostly because for the most part it's a pretty poor way of doing anything. Also, ss.clear(); ss.str(tok); would do the same thing, without being hard to read. |
Author: | OneOffDriveByPoster [ Tue Mar 04, 2008 9:51 pm ] |
Post subject: | Re: RE:Crash-Proofing.. Need help :) |
md @ Tue Mar 04, 2008 9:43 pm wrote: Also, ss.clear(); ss.str(tok); would do the same thing, without being hard to read. Ah, thanks. I never did figure out how to "reuse" a stringstream before. I guess I'll submit this to the CompSci.ca obfuscated code content.
Apologies to OP for hijacking the thread. |
Author: | LaZ3R [ Tue Mar 04, 2008 10:08 pm ] |
Post subject: | RE:Crash-Proofing.. Need help :) |
Wow I need to learn what the hell is going on in this code ... haven't learned any string stream stuff :S Thanks for the help though guys. Also thanks for that string size syntax ![]() |