I recently received an 'A' on a program I submitted for the programming course I'm currently taking.

I'm on spring break and I was hoping to use my spare time to do some studying, so I took my 'A' program and tried to run it in Visual Studio Express, and at first it ran exactly the way it had in the computers on campus. But as soon as it completed the program, the cmd window closed without allowing me time to examine the results.

I made no changes to the program, but please feel free to examine it below. I think the problem has something to do with Visual Studio. But I don't know what.

Please help?

// This program solves problems :)
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	int choice;

   double 
	   pi = 3.14159,
	   radius,
	   length,
	   width,
	   height,
	   base,
	   area;
   
	// Diplay menu and get choice
   cout << "\t\t\tPKGEOCALC Attempt #256-1\n\n";
   cout << "This program (Hopefully!) calculates the area of a given shape.\n";
   cout << "Please enter the number choice for the shape you wish to know the area of.\n\n"; 
   cout << "1. Circle\n\n";
   cout << "2. Rectangle\n\n";
   cout << "3. Triangle\n\n";
   cout << "Which program would you like to run?  ";
   cin >> choice;
   
   // Repond to the User's choice
      if(choice==1)
    {

	// Enter values for Circle
    cout << "\n\nWhat is the radius?  ";
	cin >> radius;

	// Calculate and diplay the solution
	area = pow(pi * radius, 2); 
	cout << "\n\nThe area of your circle is  " << area << endl << endl;
    }
	  else if(choice==2)
	{
   
   // Enter values for Rectangle
	cout << "\n\nWhat is the length of the rectangle?  ";
	cin >> length;
	cout << "What is the width of the rectangle?  ";
	cin >> width;

	// Solve and display result
	area = length * width;
	cout << "\n\nThe area of your rectangle is  " << area << endl << endl;
	}
	  else if(choice==3)
	{
  
   // Enter values for Triangle
	cout << "\n\nWhat is the base of the triangle?  ";
	cin >> base;
	cout << "What is the height of the triangle?  ";
	cin >> height; 

	// Solve and display result
	area = (base * height) / 2;
	cout << "\n\nThe area of your triangle is  " << area << endl << endl;
    }
	  else if(choice==42)
	  {
		  cout << "\n\nYou have entered the ultimate answer to\n\n";
		  cout << "life,\n";
		  cout << "the universe,\n";
		  cout << "and (pretty much) everything.\n\n";
		  cout << "You deserve a cookie!\nGo buy yourself a cookie and pretend I'm picking up the tab!\n\n";
	  }

	  else
	  {
		  cout << "\n\n *sigh* Typical human, always wanting choices not in the menu...\n\n";
		  cout << "The only valid choices are 1, 2, or 3\n";
		  cout << "Please re-run the program and enter a valid choice...or an easter-egg.\n\n";
		  cout << "There's a cookie in it for you if you go for the easter-egg!\n\n";
	  }


   return 0;

}

When you return from main, your program is terminated, this also means that the console window will close.

In Visual studio, if you execute your program by pressing 'Ctrl + F5' it will not immediately close, instead it will wait for you to press a key.

In other situations, you can prevent termination by trying to read from stdin (and thus waiting for user input).

When you return from main, your program is terminated, this also means that the console window will close.

In Visual studio, if you execute your program by pressing 'Ctrl + F5' it will not immediately close, instead it will wait for you to press a key.

In other situations, you can prevent termination by trying to read from stdin (and thus waiting for user input).

Thanks a lot for the speedy reply!

I have been running with 'Ctrl + F5' but it doesn't look like it's running without debugging. How would I make sure it does?

I'm quite sure it does not run under a debugger when you press ctrl+F5. What makes you think that it does?

Try adding a breakpoint on a line of code that surely will be executed. It should not break if you 'run without debugging'

I'm quite sure it does not run under a debugger when you press ctrl+F5. What makes you think that it does?

Try adding a breakpoint on a line of code that surely will be executed. It should not break if you 'run without debugging'

Please forgive my lack of experience, but I'm not sure I understood your last post. I need to add a breakpoint?

I may have misunderstood your question.

Do you want to be able to debug your program? I deduced from your last reply that you think 'ctrl+f5' will allow you to debug the code inside visual studio, and that you do not want this.

So let's summarize:
1) 'F5' - Run under debugger, window will close when returning from main
2) 'Ctrl+F5' - Do NOT run under debugger, window will not close when returning from main.

So if you want to be able to step through your code, you have to press F5 and insert a breakpoint somewhere. Your program will then stop on the breakpoint and hand control over to visual studio, so you can inspect variables that are in scope etc.

If you just want to run the program, but keep the window open when it finishes execution, press 'ctrl+5' or add something like std::cin.get(); before the return 0;

I hope it's clear now, if not please be very specific what behaviour you're looking for.

Ok.

I'm wanting the window to stay open when the program finishes executing. I have been pressing 'Ctrl + F5' from the start, but the window always closes when the program finishes.

I tried adding "std::cin.get();" on the line before the return, just as you suggested, but the cmd window still closes almost immediately after execution.

Ok, then there probably are some left-overs in the input buffer, as I see now from the code you posted you are actually requesting input from the user.

Have a look at this sticky:
http://www.daniweb.com/forums/thread90228.html

It explains nicely how you can flush the input buffer, after that both methods (cin.get() at the end or running with ctrl+f5) should work.

cin.get() will not work either if there are still keystroks in the keyboard buffer. You will have to flush them out first. Here is a thread that shows you how to do that.


[edit]Oops! same as ^^^ said.

Ok, thankyou. I'm going to look at those links later this afternoon.

But I find it interesting that I don't have any problem at all running the same program from the same software at the school. It makes me wonder if I have something set wrong on my PC.

You do not have anything set wrong on your PC. This is how Visual Studio works. Your programme gets made an an executable file. The executable file is run from the command line.

If you open a console window and run it yourself manually by typing the name of the file, the console window will remain open until you close it.

i beleive for running puposes you can add the line:

system ("PAUSE");

at the end of the program, just before the "return 0;" line.

I would like to express my thanks for all those who offered suggestions. I added the system pause at the end of my program and am able to review my program's data before cmd closes.

I still don't understand why my cmd closes in this manner, though. :(

On my campus computers, the window always remains open until I 'press any key to continue.'

Why would my PC be any different? Is there a setting I can change to make VS always leave the window open?


*I AM using ctrl + F5 BTW, to start w/o debug*


EDIT: 'theLamb' I still haven't had time yet to fully digest the link you posted, but that looks like a pretty neat solution, so thankyou.

And also try and look into switch statement ok?
Its more robust and easy to use. Besides its more easy to extend and debug.
;)

I appreciate the suggestion. Will do!


I still don't understand why my cmd closes in this manner, though. :(

On my campus computers, the window always remains open until I 'press any key to continue.'

Why would my PC be any different? Is there a setting I can change to make VS always leave the window open?


*I AM using ctrl + F5 BTW, to start w/o debug*


EDIT: 'theLamb' I still haven't had time yet to fully digest the link you posted, but that looks like a pretty neat solution, so thankyou.

The difference may be the compiler you are using. Are you using the same IDE/compiler on your home PC that you are using at school?

The difference may be the compiler you are using. Are you using the same IDE/compiler on your home PC that you are using at school?

Wow. Thanks Dragon.

I feel really stupid. My school uses VS '08. I'm using VS 2010...

A friend of mine just informed me that VS 2010 removed the feature where VS keeps the cmd window open when you start w/o debugging. :(

Oh, well...

system('PAUSE') is working fine for me...

A friend of mine just informed me that VS 2010 removed the feature where VS keeps the cmd window open when you start w/o debugging.

Your friend is incorrect, though it's no longer a default setting. You can go to the project properties, Configuration -> Linker -> System, and set the subsystem to Console. This will add a pause at the end when running from the IDE.

Your friend is incorrect, though it's no longer a default setting. You can go to the project properties, Configuration -> Linker -> System, and set the subsystem to Console. This will add a pause at the end when running from the IDE.

Oh, thank you! That solved all of my problems right there!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.