Thread: why does this crash?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The reason scripting and command line programs continue to exist and be used is because it is easier to implement advanced functionality in them than it is in a interactive program (eg IDE, GUI, etc).

    While some development environments make it easier to develop graphical or text based user interfaces than was possible in the past, it is basically easier to parse a string (eg command line input) than it is to respond to various inputs or events associated with a user interface. It is possible to develop an IDE with all the advanced features of a command-line equivalent, but the effort of getting it right (correct "look and feel", correct responses to all possible sets of inputs,all the other things that contribute to "usability") is greater. Getting those sorts of things right requires consistent rules of interface design to achieve usability (an active area of research) and then development of high quality tools that allow development of user-interfaces using those rules (IDE is another area with considerable investment happening) and then programmer discipline to use them (a requirement against which many programmers require a lot of development).

    This sort of thing is one of the reasons that vendors like Apple (MacOS attempts to provide all necessary power through GUI, so the user rarely needs to employ a command line program) charge a premium for their products. [And, no, I am not a MacOS user -- I pay less and use windows, but find I must sometimes resort to the command line].
    Last edited by grumpy; 05-21-2008 at 05:48 AM.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Both Microsoft and Apple, however, are pushing forward GUIs. I don't know about Apple, but Microsoft is certainly trying to push their own techologies on us to make it easier to develop feature-rich and pretty GUIs.
    In the future, the line between command line and GUIs is going to blur even more so, I think. And yet, will Linux still retort to their mostly command-line approach, I wonder?
    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
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Elysia View Post
    In the future, the line between command line and GUIs is going to blur even more so, I think. And yet, will Linux still retort to their mostly command-line approach, I wonder?
    Not precisely true. There are some versions of linux (ubuntu comes to mind) that have a goal of an easier to use desktop (ubuntu is based on Gnome, IIRC). Not as far advanced as windows or MacOS, but they are advancing in that direction. Also MacOS is implemented on top of a BSD Unix.

    Command line versus GUI is actually a trade-off, not an absolute case of right versus wrong. GUI front ends appeal more to non-expert users than command lines, so we can expect the practice of designing and implementing GUI to advance. However, it will be a long time before the effort to implement a GUI program will be comparable to the effort to implement a command line program that delivers equivalent functionality -- simply because the fundamentals of user interface, in current state of the art, involve more concepts than command line.

    My personal prediction is that there will always be a significant role for both GUI and command line programs. From a design perspective, a well designed functional interface/separation/boundary between user interface and the supporting business logic is always desirable, rather than mixing elements of business logic into the user interface: it allows the user interface and business logic to be updated without breaking the other. This separation of interface and business logic is also fundamental to design and implementation of distributed applications - another happening trend. Two common ways of separating user interface from business-logic are (1) a user interface front end that employs a command-line based back-end and (2) a user interface front end and a command-line based front end, that both employ a common business logic back-end (eg using the business logic in a common library).

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by manav View Post
    Just understand one thing, command line and script tools are like a powerful programming language. You can never express in any kind of advanced GUI what you can express in a programming language, for example, say C++.
    Nay.
    A GUI can express a lot of things a scripted language can, but it depends on the complexity and the aim. Expressing C++ is a GUI would be next to impossible (but DOABLE), but if you have the time and determination, it's possible. I dare say it would be a quality program too, if done right.

    The GUI and command line/script tools have completely different benefits and fields of work. Period.
    No, no period.
    It can be done either ways.
    Sure, you can make a compiler command line, but then again, you can make it as a dll or another form of code base - equally possible. It's just that we do not do this for various reasons - but it doesn't mean we cannot, or that it is targetted at a different field of work.
    It may just be a bad habit, or disadvantages to it, but it's certainly possible.

    You can say it's legacy. We still use old units for physics, for example, or food, even though newer, better units exist. Legacy.
    Last edited by Elysia; 05-21-2008 at 06:12 AM.
    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