
-----------------------------------
LaZ3R
Tue Mar 04, 2008 6:49 pm

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 :P

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 :) Thanks guys.

-----------------------------------
HeavenAgain
Tue Mar 04, 2008 6:54 pm

RE:Crash-Proofing.. Need help :)
-----------------------------------
[url=http://cis.stvincent.edu/carlsond/swdesign/except/except.html]exceptions handling

-----------------------------------
nike52
Tue Mar 04, 2008 7:51 pm

Re: Crash-Proofing.. Need help :)
-----------------------------------
 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 :) Thanks guys.

http://cplusplus.com/reference/string/string/size.html

Make it a string first and then use str.size().

-----------------------------------
OneOffDriveByPoster
Tue Mar 04, 2008 7:57 pm

Re: Crash-Proofing.. Need help :)
-----------------------------------
Write your own version of the following (because it is messier than necessary):
#include 
#include 
#include 

using namespace std;

int main(void) {

    string tok; char junk; stringstream ss;
    double n;

    while ( cin >> tok, cin && (ss.~stringstream(), new (&ss) stringstream(tok), (ss >> n) && !(ss >> junk)) ) {
        cout  tok, cin && (ss.~stringstream(), new (&ss) stringstream(tok), (ss >> n) && !(ss >> junk)) ) {
    cout  fl))
{
	std::cin.clear();
	std::cin.ignore(std::numeric_limits::max(),'\n');
}


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.

-----------------------------------
OneOffDriveByPoster
Tue Mar 04, 2008 8:29 pm

Re: RE:Crash-Proofing.. Need help :)
-----------------------------------
And Saad is correct, OneOffDriveByPoster's code has a nice bug fat memory leak in it.Show me.

-----------------------------------
Saad
Tue Mar 04, 2008 8:40 pm

Re: RE:Crash-Proofing.. Need help :)
-----------------------------------
And Saad is correct, OneOffDriveByPoster's code has a nice bug fat memory leak in it.Show me.

new (&ss) stringstream(tok)

Where is this memory freed?

-----------------------------------
OneOffDriveByPoster
Tue Mar 04, 2008 8:42 pm

Re: RE:Crash-Proofing.. Need help :)
-----------------------------------
And Saad is correct, OneOffDriveByPoster's code has a nice bug fat memory leak in it.Show me.

new (&ss) stringstream(tok)

Where is this memory freed?
The memory was not dynamically allocated.

-----------------------------------
md
Tue Mar 04, 2008 9:11 pm

Re: RE:Crash-Proofing.. Need help :)
-----------------------------------
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:


#include 
#include 
#include 

using namespace std;

int main(int argc, char **argv)
{
    string tok, junk;
    stringstream ss;
    float n;
    while ( cin >> tok, cin && (ss.~stringstream(), new (&ss) stringstream(tok), (ss >> n) && !(ss >> junk)) ) 
    	cout  tok )
    {
    	if( cin )
    	{
    		ss.~stringstream();
    		new (&ss) stringstream(tok);
    		ss >> n;
    		cout > tok, cin && (ss.~stringstream(), new (&ss) stringstream(tok), (ss >> n) && !(ss >> junk)) ) {
        cout > tok ) {
        ss.~stringstream();
        new (&ss) stringstream(tok);
        if ( (ss >> n) && !(ss >> junk) ) {
            cout 