Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Visual C++ Quirks

Options
  • 28-01-2008 9:21pm
    #1
    Closed Accounts Posts: 14


    Hi guys,

    I've been doing some very simpe program examples from the net and a c++ book i bought and found it quite hard to get them to compile. Doing the simplest things such as displaying stuff to a user with cout<< ive found that the program runs for like a millisecond and then ends. I was only lucky that i remember a friend who used some type of pause function at the end of the program which i figured out to be cin.get() which i placed at the end of the code.
    Also when getting input from a user (cin>> ...etc) in a simple console application ive found that its necessary to add cin.get() after the cin>> line.
    Why is this not shown or explain in books or on the net in tutorials etc or is it a case of the program im using MS Visual Studio being a bit awkward.
    Does anyone know any other of these little 'quirks' cos they are fairly time consuming. Cheers


Comments

  • Closed Accounts Posts: 413 ✭✭spooydermot


    The problem with the program disappearing after a millisecond is that it's running in a dos-box , finishes and then exits

    Try going to the command line (Start -> Run - > cmd) and running the program from there (obviously going to the correct diretory)


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Try a :

    System("pause");

    I believe buffers and everything will start getting confusing if you use a cin.get() - this I believe gets a single charactor if I remember correctly.

    Try the System("PAUSE"); and see how you get on.

    Technically System("pause") is not best as your code will no longer be compatible with other OS's. Anyways should do the trick while you are learning.


  • Registered Users Posts: 413 ✭✭ianhobo


    Technically, all of the code that is in the program has run, so why should the window stay open?

    Here's the trick...
    inlude:
    #include <stdio.h>  //C standard header
    

    and add this at the end of your code where you want it to stop/pause
    getchar();
    

    As opposed to using the cin technique which will only work if you actually enter something on the keyboard and hit enter, with getchar(), all you have to do is hit enter or any key, and the program will exit :D


  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    Assuming you're just hitting F5 to run the program, which effectively debugs it and as long as there's no breakpoints will continue to end and quit. Try running your programs as "CTRL+F5", it should run till end and the Dos box should have a "Press any key to end".


Advertisement