8 Replies - 5071 Views - Last Post: 06 January 2011 - 06:36 AM Rate Topic: -----

#1 xorangejuicex  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 16
  • Joined: 31-August 10

Console Window Closing Immediately

Posted 06 January 2011 - 12:57 AM

How do I keep my program from closing right away? I'm following tutorials from a book and when I build and debug it the program closes instantly.

For example:
#include <iostream>
#include <iomanip>

using std::cout;
using std::endl;
using std::setw;

int main()
{
	int num1 = 1234, num2 = 5678;
	
	cout << endl;
	cout << setw(6) << num1 << setw(6) << num2;
	cout << endl;
	return 0;
}


This would just open and close the console app immediately. I have some experience in a different language, so this hasn't been a problem really because I know what it's supposed to show, but now that I'm getting into a little more complex things I would like to actually look at the results of my code.

Is This A Good Question/Topic? 0
  • +

Replies To: Console Window Closing Immediately

#2 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5345
  • View blog
  • Posts: 27,296
  • Joined: 10-May 07

Re: Console Window Closing Immediately

Posted 06 January 2011 - 01:00 AM

This has been discussed under the C/C++ FAQs : Holding the Execution Window Open.
Was This Post Helpful? 1
  • +
  • -

#3 xorangejuicex  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 16
  • Joined: 31-August 10

Re: Console Window Closing Immediately

Posted 06 January 2011 - 01:03 AM

Oh sorry, I didn't even think to look there. Thank you for pointing me to it, I'll be sure to check there before I post more questions like this.
Was This Post Helpful? 2
  • +
  • -

#4 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5345
  • View blog
  • Posts: 27,296
  • Joined: 10-May 07

Re: Console Window Closing Immediately

Posted 06 January 2011 - 01:10 AM

No worries! Let us know if you have any questions :)
Was This Post Helpful? 0
  • +
  • -

#5 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Console Window Closing Immediately

Posted 06 January 2011 - 02:37 AM

getchar() works well for me, but cin.get() will usually work just as well, if you put a cin.ignore() just before it.
Was This Post Helpful? 0
  • +
  • -

#6 sas1ni69  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 431
  • Joined: 04-December 08

Re: Console Window Closing Immediately

Posted 06 January 2011 - 03:21 AM

#include <iostream>
#include <iomanip>

using std::cout;
using std::endl;
using std::setw;

int main()
{
	int num1 = 1234, num2 = 5678;
	
	cout << endl;
	cout << setw(6) << num1 << setw(6) << num2;
	cout << endl;

        system("pause"); //This works just fine.
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: Console Window Closing Immediately

Posted 06 January 2011 - 04:44 AM

No, you should NOT use system("PAUSE");

Also, I changed your title. "Just a Quick Question" is not a good topic title. We KNOW you've got a question, it's why you posted! Give a short descriptive title when starting topics.

This post has been edited by JackOfAllTrades: 06 January 2011 - 04:52 AM

Was This Post Helpful? 0
  • +
  • -

#8 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5345
  • View blog
  • Posts: 27,296
  • Joined: 10-May 07

Re: Console Window Closing Immediately

Posted 06 January 2011 - 05:21 AM

lol, works just fine.

Yeah, assuming one is running a shell with a pause command. DERP!
Was This Post Helpful? 0
  • +
  • -

#9 mybytes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-January 11

Re: Console Window Closing Immediately

Posted 06 January 2011 - 06:36 AM

Another way is to open up the command line and compile it manually-- the window will remain open when the program finishes. Though that doesn't help if you are using an IDE.

I hope everything is going well for you!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1