CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2014
    Posts
    17

    Beginner- simple question

    I'm just beginning to code in C++.

    I created an empty project in Visual Studi and then created a source file (cpp file) and then added the follwoign bit of code

    Code:
    #include <iostream>
    
    using namespace std;
    
    class Box
    {
       public:
          double length;   // Length of a box
          double breadth;  // Breadth of a box
          double height;   // Height of a box
    };
    
    int main( )
    {
       Box Box1;        // Declare Box1 of type Box
       Box Box2;        // Declare Box2 of type Box
       double volume = 0.0;     // Store the volume of a box here
     
       // box 1 specification
       Box1.height = 5.0; 
       Box1.length = 6.0; 
       Box1.breadth = 7.0;
    
       // box 2 specification
    
       Box2.height = 10.0;
       Box2.length = 12.0;
       Box2.breadth = 13.0;
       // volume of box 1
       volume = Box1.height * Box1.length * Box1.breadth;
       cout << "Volume of Box1 : " << volume <<endl;
    
       // volume of box 2
       volume = Box2.height * Box2.length * Box2.breadth;
       cout << "Volume of Box2 : " << volume <<endl;
       return 0;
    }
    When I debug the print statements (cout) appear at the command prompt (black console window) and the window soon disappears.

    I do not the window to disappear until I close it.

    How can this be done?

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Beginner- simple question

    There are various ways of doing this. One way is to add at the end
    Code:
    char ch;
    cin >> ch;
    or
    Code:
    cin.get();
    Press <CR> to continue.

    Another way is
    Code:
    system("pause");
    what os are you using?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Apr 2014
    Posts
    17

    Re: Beginner- simple question

    Windows 7

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Beginner- simple question

    if you put
    Code:
    #include <conio.h>
    at the top of the program, then you can use
    Code:
    getch();
    which requires any char to be entered to continue, not just <CR> as with get().
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Beginner- simple question

    You should be able to configure Visual Studio to pause the console screen for you. Unfortunately, I do not know how as it has always been the default for those times when I have used that IDE.

    Alternatively, you can open a console window and run your program from there. Since the console window was opened separately, it will not close when your program exits.

    The workarounds would be as 2kaud described, i.e., to prompt for input at the end of the program such that the program will not exit and hence the console window will remain open, or to invoke a shell command to pause the program at the end. I call them workarounds since they would be awkward when you actually do properly run the program from a separate console window.

    Note that <conio.h> and getch are non-standard.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: Beginner- simple question

    In VS2010, it's "Debug - Start without Debugging" or Ctrl + F5.
    In other versions, it might be similar.

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Beginner- simple question

    Instead of adding code to pause the window (which you might forget to remove), just use CTRL + F5 or add a breakpoint on the return 0; line in main. When the breakpoint is hit, ALT + TAB to switch to the console window.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured