Help for a newbie

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • drude
    New Member
    • Jul 2007
    • 3

    Help for a newbie

    hi,
    Im totally new to computer programming infact C++ is my first programming language and i just compiled and ran my first program-hello world..
    now my problem is that im not able to run the exe file i got after the compiling an linking..when i run the file the window just opens and closes without me even getting a chance to see whats printed..i use dev-cpp ide on windows xp..if it has anything to do with the ide im using could u please suggest some other ide or compiler???

    thanks
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by drude
    hi,
    Im totally new to computer programming infact C++ is my first programming language and i just compiled and ran my first program-hello world..
    now my problem is that im not able to run the exe file i got after the compiling an linking..when i run the file the window just opens and closes without me even getting a chance to see whats printed..i use dev-cpp ide on windows xp..if it has anything to do with the ide im using could u please suggest some other ide or compiler???

    thanks
    provide a getchar() call at the end of the program and run it.

    Raghuram

    Comment

    • archonmagnus
      New Member
      • Jun 2007
      • 113

      #3
      Since you are using windows, you can also use:
      [code=cpp]
      // Windows only
      system("pause") ;
      [/code]
      or

      [code=cpp]
      // Windows only
      // with the other preprocessor directives
      #include <conio.h>

      // at the end of file
      cout<<"Press any key to exit."<<endl;
      getch();
      [/code]

      Though the latter is generally not recommended, it works all the same. Putting a get or a getchar is the best way, but these will also work.

      Comment

      • momotaro
        Contributor
        • Sep 2006
        • 357

        #4
        and yet another metod: use an infinit loop it is not the best way to do it but the result is there.
        or ask the user if he wants to reuse your program then loop using a sentinel

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          I can't believe that that particular IDE doesn't have a flag or something for just that.
          Using a getchar() just before main() returns seems extremely silly to me. What if
          you forget to take that call out just before your start deploying? There must be an
          option availabe in that IDE otherwise its makers deserve to have their head nailed
          to the floor ;-)

          kind regards,

          Jos

          Comment

          • drude
            New Member
            • Jul 2007
            • 3

            #6
            hi,
            thank you all,getch() worked...is it normal,i mean is the exe supposed to just flash the result and close??

            Comment

            • drude
              New Member
              • Jul 2007
              • 3

              #7
              Originally posted by JosAH
              I can't believe that that particular IDE doesn't have a flag or something for just that.
              Using a getchar() just before main() returns seems extremely silly to me. What if
              you forget to take that call out just before your start deploying? There must be an
              option availabe in that IDE otherwise its makers deserve to have their head nailed
              to the floor ;-)

              kind regards,

              Jos
              thank you for taking the time to reply..
              As i mentioned in my question im quite new to this and half the things you said above are greek to me but still I'd like to know what you meant..for instance flags?what happens if i forget to take that call out just before you start deploying?

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Originally posted by drude
                As i mentioned in my question im quite new to this and half the things you said above are greek to me but still I'd like to know what you meant..for instance flags?what happens if i forget to take that call out just before you start deploying?
                What JosAH means is that you should have any code in your program that enables you to debug the code. That getchar() at the end requires keystrokes. If you mail your finished program to customers and they all have to press keys to stop the program, they aren't going to be happy.

                Check your tool. There has to be a feature to pause at the end of main() withou special code.

                Comment

                • archonmagnus
                  New Member
                  • Jun 2007
                  • 113

                  #9
                  Originally posted by drude
                  hi,
                  thank you all,getch() worked...is it normal,i mean is the exe supposed to just flash the result and close??
                  That's just Windows XP killing the window after the return. If you open a cmd (DOS) window, navigate to the working directory of your executable, and run it, Windows will return to the "shell" after the return value (allowing you to see the output.)

                  Note that if you are indeed using getch(), that conio.h isn't a cross platform library. It will work fine for "newbies" (as you claim to be), but as you endeavour-to-persevere (so to speak) in your programming, you will doubtless encounter/implement more sophisticated methods. ;)

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    Originally posted by drude
                    when i run the file the window just opens and closes without me even getting a chance to see whats printed..i use dev-cpp
                    I don't know about dev-cpp, but I do know Visual Studio.NET 2005.

                    With Visual Studio, if you select "Start" torun your code, you will see the console window flash and disappear just as you are describing.

                    That's because Visual Studio is making debuggable programs (called a debug build). These .exe's have code put there to run the debugger. When the program starts, Visual Studio assumes you are using your debugger and have taken care of inserting the necessary breakpoints. So it just runs your code.

                    However, you can also select "Start Without Debugging". Now you are telling Visual Studio that while this is a debuggable program you are not using your debugger. Now Visual Studio will pause at the end of main and display "Press any key to continue..." and wait for a keystroke.

                    Is there a similar alternate start selection for dev-cpp???

                    Comment

                    Working...