-
April 16th, 2014, 05:57 AM
#1
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?
-
April 16th, 2014, 06:09 AM
#2
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
Press <CR> to continue.
Another way is
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)
-
April 16th, 2014, 06:57 AM
#3
Re: Beginner- simple question
-
April 16th, 2014, 07:08 AM
#4
Re: Beginner- simple question
if you put
at the top of the program, then you can use
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)
-
April 16th, 2014, 11:33 AM
#5
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.
-
April 17th, 2014, 01:29 PM
#6
Re: Beginner- simple question
In VS2010, it's "Debug - Start without Debugging" or Ctrl + F5.
In other versions, it might be similar.
-
April 17th, 2014, 08:58 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|