Programm doesn't work right

This is a discussion on Programm doesn't work right within the C++ Programming forums, part of the General Programming Boards category; I just now got myself a C++ book and wanted to start learning. I got Microsoft Visual C++ Express 2005 ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    2

    Programm doesn't work right

    I just now got myself a C++ book and wanted to start learning. I got Microsoft Visual C++ Express 2005 and I found the typical first program in the book

    Code:
    // a small C++ program
    #include <iostream>
    
    int main()
    {
        std::cout << "Hello, world!" << std::endl;
        return 0;
    }
    well I compiled and got the .exe. but it only opens for a millisecond and closes right away. The book says it should actually not do that. So what am I doing wrong? Plz don't be to complicated im a noob.

    PS: I tried searchig but somehow there was nothing

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    2
    Well thats what I meant but how do I do I do those things and why is it printed in a book if it won't work?

    Ok I put in the getch() command and it works but do I have to do this with everything it says in the book?
    Last edited by N0n@m3; 07-08-2007 at 06:04 AM.

  4. #4
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    I don't use getch() myself, I use std::cin.get();

    Code:
    // a small C++ program
    #include <iostream>
    
    int main()
    {
        std::cout << "Hello, world!" << std::endl;
        std::cin.get();
        return 0;
    }
    I don't know which one is better, but yes, you would have to use either one with every program in the book if they don't use them.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    Because normally, console programs are run from the command line, not from within an IDE.

    So you would type at your ($) prompt
    $ myprog.exe

    Which would produce your output, and then show the prompt ready for your next command.

    It is possible (AFAIK) to configure some IDE's to automatically hold the final screen of a console program as it exits, without any kind of 'getchar' pause.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    24
    in visual c++ when your making the program next to the line return 0 click on the side
    then a dot should appear this will stop the program from auto closing

    if you could post a picture of your workspace I could circle the area

  7. #7
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    Quote Originally Posted by tbca View Post
    in visual c++ when your making the program next to the line return 0 click on the side
    then a dot should appear this will stop the program from auto closing
    When doin that you are setting a break-point, which is irrelevant if you ask me.
    Try to use one of the other methods mentioned.

  8. #8
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    In VC if you choose "run without debugging"(Ctrl -F5) then the command window won't close automatically

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by N0n@m3 View Post
    Well thats what I meant but how do I do I do those things and why is it printed in a book if it won't work?
    Thats just how windows works with console programs - If you were to try any other console-based program from within windows (Note - not from within the command box), exactly the same will happen - for example, type "ipconfig" in your start->run box, the program runs, and disappears just as quickly, before you get a chance to see the output.

    use Darryl's suggestion of pressing CTRL+F5 to compile & run your programs from within MSVC++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  3. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 03:48 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21