Thread: why does this crash?

  1. #1
    Village id10t
    Join Date
    May 2008
    Posts
    57

    why does this crash?

    this compiles but crashes! i cant find my mistake (this is the entire code so far)

    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    int main(){
    
    vector<int> list;
    int counter,temp;
    
    
    do{
    cout<<"Please enter a list of number, -1 to quit"<<endl;
    cin>>temp;
    list.push_back(temp);
    }while(temp!=-1);
    
    
    return 0;
    }
    ps: it gives no error code, just opens and close instantly
    Last edited by MarlonDean; 05-19-2008 at 03:48 AM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Probably because you're not entering -1, so the loop quits. Once the loop quits, the program then quits since there is nothing left to do but return 0. There is no crashing. It's just ending because you didn't write your loop condition proplery.

  3. #3
    Village id10t
    Join Date
    May 2008
    Posts
    57
    but isnt it suppose do to the cout statements at least once? Is that the point of using a do... while?

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Actually, I misread that. I must be a little blind tonight. The code looks relatively fine, outside of poor indention.

    I even ran it myself, and it appears to work.... I don't know what your problem is. Sorry.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Aside from going into an infinite loop on entering a non-number, the code is fine. Are you sure that's exactly what you're compiling?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    This isn't your problem here, but avoid "using namespace std", especially when declaring variable names that are type names in the standard namespace (e.g. list). It's a bad habit to get into.

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    3
    I dont know works perfect to me try using a different compiler

  8. #8
    Village id10t
    Join Date
    May 2008
    Posts
    57
    Yes, the problem is my compiler, if i save a file, i must not have a space in the filename eg. project 1.cpp will cause my program to flash

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by MarlonDean View Post
    Yes, the problem is my compiler, if i save a file, i must not have a space in the filename eg. project 1.cpp will cause my program to flash
    I'm sure the compiler will cope with spaces in the filename, but you may need to quote the filename, e.g.
    Code:
     
    gcc "my file.c"
    If you use:
    Code:
    gcc my file.c
    the compiler will try to compile "my" and "file.c" which will not do the right thing for you if you have a file called "my file.c".

    If you use an IDE that doesn't cope with spaces in filenames, that's a bug in the IDE.

    Finally, I would say it's a BAD idea to use filenames (or directories) with spaces for programming projects. It tends to lead to all sorts of interesting effects.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by matsp View Post
    Finally, I would say it's a BAD idea to use filenames (or directories) with spaces for programming projects. It tends to lead to all sorts of interesting effects.
    On a side note, Visual Studio seems to handle spaces in files fine. MFC sometimes tends to generate files with spaces inside. I can't vouch for other IDEs/compilers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Welcome to the future, manav. The future where IDEs compile our files for us so we don't need to use scripts or command line.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by manav View Post
    Someone (I can't) please, explain to Elysia, that, using command line or automated scripts is not akin to being in past. These are also advanced techniques. Gosh!
    Both have their benefits. There are some really powerful solutions using command-line tools. Other things are much easier and better done in the IDE. It's not an "all or nothing" situation, there is a choice, and it's not that you can't choose for each individual task which one would work best.

    A lot of people who don't use command line do so because they simply have never learned how to use the tools that are available there. There's also the problem that many of the OLD DOS and Windows equivalents of the Unix tools where not quite as good as the Unix equivalents.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by manav View Post
    Someone (I can't) please, explain to Elysia, that, using command line or automated scripts is not akin to being in past. These are also advanced techniques. Gosh!
    Command lines are definitely outdated now, especially for Windows (maybe not for Linux).
    There are tools that can generate scripts and the like, so you won't have to do it by hand. A good GUI will not care if the filenames contain spaces or not.

    A library that requires you to compile your project via a makefile is a bad library. A good library can optionally support both.

    Command lines are tricky and error prone (remember screwing up C++ exception handling due to compiling via the command line?).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Command lines are definitely outdated now, especially for Windows (maybe not for Linux).
    There are tools that can generate scripts and the like, so you won't have to do it by hand. A good GUI will not care if the filenames contain spaces or not.

    A library that requires you to compile your project via a makefile is a bad library. A good library can optionally support both.

    Command lines are tricky and error prone (remember screwing up C++ exception handling due to compiling via the command line?).
    That is your personal opinion, and you are entitled to it. I use command line a lot in Windows, it works just fine and does what you want, and building Windows drivers is completely done with command-line tools provided by the Windows DDK. Yes, you can make the Visual Studio IDE run the command line tools for you, but it's not running the standard IDE build tools.

    As to getting options correct, you can get that right and wrong in both command line and IDE - if you don't set the right options [and figuring out WHAT IS RIGHT is a bit part of that puzzle] in either of them, you don't get what you wanted. It makes absolutely no difference if you have to select a box in an IDE or type in /EHsc [or whatever option you happen to want to use] on the command line - whichever way you do it, you still need to get it right.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by matsp View Post
    As to getting options correct, you can get that right and wrong in both command line and IDE - if you don't set the right options [and figuring out WHAT IS RIGHT is a bit part of that puzzle] in either of them, you don't get what you wanted. It makes absolutely no difference if you have to select a box in an IDE or type in /EHsc [or whatever option you happen to want to use] on the command line - whichever way you do it, you still need to get it right.
    While the original thing I was aiming for was that the IDE will set the default options for you and pass them to the compiler wheras with the command-line compiler, you have to set those manually because those options will default to OFF or whatever if you don't specify them.
    So it's less error prone to use visual studio to compile instead of the compiler. This fact was eventualized by the fact that manav compiled wrong because he was given the incorrect information (or as some say, failed to read the manual) about which command lines should be used. Had he compiled using the IDE instead, he would have received no such thing.
    (Further to note that he did ignore the warning generated by the compiler also suggests that he failed to read the manual properly.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hooking Crash?
    By Elysia in forum Windows Programming
    Replies: 9
    Last Post: 03-15-2008, 01:13 PM
  2. Can not debug a crash
    By hannibar in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2007, 10:02 AM
  3. Dynamic array sizing causes crash
    By Mithoric in forum C++ Programming
    Replies: 3
    Last Post: 12-30-2003, 07:46 AM
  4. FYI: asctime(gmtime(&mytime)) = crash!
    By anonytmouse in forum C Programming
    Replies: 2
    Last Post: 09-29-2003, 02:24 AM
  5. Crash after crash!
    By Dual-Catfish in forum C++ Programming
    Replies: 1
    Last Post: 03-31-2002, 09:32 AM