Sign in to follow this  
kjb281

C++ compiler question

Recommended Posts

Well, recently I've been learning some c++. I'm still VERY new with only a few days of experience. At the moment I use Bloodshed. Before whenever I ran a program it would stay up. Now when I run a program it "flashes" once really fast and I'm unable to see if my program is running correctly. I'm sure this is a noob question but how can I make it so that it stays up. My code: // defined constants: calculate circumference #include <iostream> using namespace std; #define PI 3.14159 #define NEWLINE '\n' int main () { double r=5.0; // radius double circle; circle = 2 * PI * r; cout << circle; cout << NEWLINE; return 0; }

Share this post


Link to post
Share on other sites
When developing console applications, they run in the console. Unfortunately, as soon as the program terminates, so does the console window. The obvious solution is to run the executable from the console itself. I'm not too familiar with Dev-C++, but in Visual Studio you can also set a debug breakpoint at the end of main, which will get hit right before the program terminates, so you can check the console output. Visual Studio also has the "Start without Debugging" (ctrl+f5) option, which will leave the console window open. Look for similar things in your IDE.

Share this post


Link to post
Share on other sites
put at the bottom of the main function: system("PAUSE"); I think that might be specific for dev-c++, dunno if that works in visual studio or not. But prev poster is right ctrl+f5 for vstudio.

Or if you want can always do:


string finish; //make sure to have #include <string>
cout << "Please type any key to continue" << endl;
cin >> finish;



Share this post


Link to post
Share on other sites
Quote:
Original post by agm_ultimatex
put at the bottom of the main function: system("PAUSE"); I think that might be specific for dev-c++, dunno if that works in visual studio or not.


It's a Windows specific beast, not a compiler specific one.

That said, I recommend against it. It's a bad habit to get in, and all around bad form IMO. More info.

Share this post


Link to post
Share on other sites
#defines in C++ are generally reserved for header guards. You appear to want named constants. Luckily, the language provides just the thing. I also included a few other comments in the code:

#include <iostream>
using namespace std;

const double PI = 3.14159;
const char NEWLINE = '\n';

int main()
{
// don't make a variable called 'r' and then add a comment 'radius'
// just name the variable 'radius'. =)
double radius = 5.0;

// prefer to initialise variables at the same time as you declare them
// double circle;

// like so
double circle = 2 * PI * radius;

// we can "chain" calls to std::cout, allowing us to do this in one line
cout << circle << NEWLINE;

// main() is a special function: it will always return 0
// unless you specify otherwise
// return 0;
}

Share this post


Link to post
Share on other sites
Cool, thanks for all the help. I haven't been able to find something like that in Bloodshed but I also have Microsoft Visual Studio C++ 2008 Express Edition so I tried some of your ideas and they worked!

Thanks for all the Help!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this