Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Crash-Proofing.. Need help :)
Index -> Programming, C++ -> C++ Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
LaZ3R




PostPosted: 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 Razz

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 Smile

Any help or point in the right direction as to what variables I may need or libraries would be nice Smile Thanks guys.
Sponsor
Sponsor
Sponsor
sponsor
HeavenAgain




PostPosted: Tue Mar 04, 2008 6:54 pm   Post subject: RE:Crash-Proofing.. Need help :)

exceptions handling
nike52




PostPosted: 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 Smile

Any help or point in the right direction as to what variables I may need or libraries would be nice Smile Thanks guys.


http://cplusplus.com/reference/string/string/size.html

Make it a string first and then use str.size().
OneOffDriveByPoster




PostPosted: 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):
c++:
#include <iostream>
#include <sstream>
#include <new>

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 << n << endl;
    }

    cout << "Not valid double value or input error; bye!" << endl;
    return 0;
}
Saad




PostPosted: Tue Mar 04, 2008 8:02 pm   Post subject: RE:Crash-Proofing.. Need help :)

cplusplus:
while ( cin >> tok, cin && (ss.~stringstream(), new (&ss) stringstream(tok), (ss >> n) && !(ss >> junk)) ) {
    cout << n << endl;
}


Is it me or do I see a memory leak at
cplusplus:

new (&ss) stringstream(tok)
md




PostPosted: Tue Mar 04, 2008 8:21 pm   Post subject: RE:Crash-Proofing.. Need help :)

From http://www.augustcouncil.com/~tgibson/tutorial/iotips.html

c++:

#include<limits> //for numeric_limits

float fl;
while(!(std::cin >> fl))
{
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<streamsize>::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




PostPosted: 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.
Saad




PostPosted: 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.


cplusplus:
new (&ss) stringstream(tok)


Where is this memory freed?
Sponsor
Sponsor
Sponsor
sponsor
OneOffDriveByPoster




PostPosted: 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.


cplusplus:
new (&ss) stringstream(tok)


Where is this memory freed?

The memory was not dynamically allocated.
md




PostPosted: 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:

c++:

#include <string>
#include <stringstream>
#include <iostream>

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 << n << endl;
   
    return 0;
}


Compiling this gives the following errors:
code:
[john@saxifrage build]$ g++ test.cpp
test.cpp:2:24: error: stringstream: No such file or directory
test.cpp: In function ?int main(int, char**)?:
test.cpp:10: error: aggregate ?std::stringstream ss? has incomplete type and cannot be defined
test.cpp:12: error: invalid use of incomplete type ?struct std::stringstream?
/usr/lib/gcc/i686-pc-linux-gnu/4.2.3/../../../../include/c++/4.2.3/iosfwd:83: error: declaration of ?struct std::stringstream?


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
c++:
    while( cin >> tok )
    {
        if( cin )
        {
            ss.~stringstream();
            new (&ss) stringstream(tok);
            ss >> n;
            cout << n;
        }
    }


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.
OneOffDriveByPoster




PostPosted: Tue Mar 04, 2008 9:13 pm   Post subject: Re: RE:Crash-Proofing.. Need help :)

md @ Tue Mar 04, 2008 9:11 pm wrote:
c++:

#include <string>
#include <stringstream>
#include <iostream>
*cough* you seem to have introduced typos into my code
md




PostPosted: 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? Razz

Also... appologies for the infinite posts.
OneOffDriveByPoster




PostPosted: 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? Razz

Also... appologies for the infinite posts.
That's fine. I'll edit this post as I put it together. For now the key is to "google" lib.new.delete.placement

New version remains somewhat obfuscated, but I believe that is necessary to enable comparison between the different versions.

c++:
#include <iostream>
#include <sstream>
#include <new>

using namespace std;

int main(void) {

    string tok; char junk; stringstream ss;
    double n;

#if OBFUSCATED
    while ( cin >> tok, cin && (ss.~stringstream(), new (&ss) stringstream(tok), (ss >> n) && !(ss >> junk)) ) {
        cout << n << endl;
    }
#else
    // less obfuscated, but still "messier than necessary"
    while ( cin >> tok ) {
        ss.~stringstream();
        new (&ss) stringstream(tok);
        if ( (ss >> n) && !(ss >> junk) ) {
            cout << n << endl;
        }
        else {
            break;
        }
    }
#endif

    cout << "Not valid double value or input error; bye!" << endl;
    return 0;
}


Post is done.
Saad




PostPosted: 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.
md




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 17 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: