Jump to content


Photo

Program Closes Right Away


  • Please log in to reply
5 replies to this topic

#1 Hoot

Hoot

    * Hooty Tooty Fruit *

  • Members
  • PipPipPipPip
  • 1,346 posts
  • Gender:Male
  • Location:Canada
  • Interests:Making Websites, Working in 3D, Playing the Drums

Posted 26 July 2006 - 04:50 PM

I JUST started to learn C++ and I cant figure out why my program always closes right away when I run it.
here is my code:
#include <iostream>
using namespace std;

int main()
{
	int num = 100;
	char letter = 'M';
	float decimal = 7.5;
	double pi = 3.14159;
	bool flag = false;
	
	cout << "num:\t" << num << endl;
	cout << "letter:\t" << letter << endl;
	cout << "decimal:\t" << decimal << endl;
	cout << " precise:\t" << pi << endl;
	cout << "flag\t" << flag << endl;
	return 0;
}

Hoot

#2 PSgirl

PSgirl

    P2L Staff

  • P2L Staff
  • PipPipPipPip
  • 2,056 posts
  • Gender:Female

Posted 28 July 2006 - 07:04 AM

You can add
cin.get();cin.get();
at the end, just before the return:

#include <iostream>
using namespace std;

int main()
{
	int num = 100;
	char letter = 'M';
	float decimal = 7.5;
	double pi = 3.14159;
	bool flag = false;
	
	cout << "num:\t" << num << endl;
	cout << "letter:\t" << letter << endl;
	cout << "decimal:\t" << decimal << endl;
	cout << " precise:\t" << pi << endl;
	cout << "flag\t" << flag << endl;

	cin.get(); cin.get();
	return 0;
}

So this way the program waits until you press some key two times.
I think you can also use
system("pause");
But I've always used the cin.get() thing :)
Hope it helps.

Edited by PSgirl, 28 July 2006 - 07:05 AM.


#3 Hoot

Hoot

    * Hooty Tooty Fruit *

  • Members
  • PipPipPipPip
  • 1,346 posts
  • Gender:Male
  • Location:Canada
  • Interests:Making Websites, Working in 3D, Playing the Drums

Posted 28 July 2006 - 09:54 AM

It worked, thanks a ton! ^_^ :D

Hoot

#4 PSgirl

PSgirl

    P2L Staff

  • P2L Staff
  • PipPipPipPip
  • 2,056 posts
  • Gender:Female

Posted 28 July 2006 - 02:12 PM

np, glad to help ^_^
C++ can be a bit abstract at first :D

#5 Indigo

Indigo

    Official Alien

  • Members
  • PipPipPip
  • 617 posts
  • Gender:Male
  • Location:Trondheim, Norway
  • Interests:Computing in general, especially design and programming of all kinds.

Posted 02 August 2006 - 09:44 AM

I thinkyou could use cin.ignore(); or something too, but I use system("pause");

#6 Flixified

Flixified

    Young Padawan

  • Members
  • Pip
  • 6 posts

Posted 02 August 2006 - 07:30 PM

Also, if you open up a command prompt and navigate to the folder where the EXE is you can run it without it closing. The return 0; is what makes the program end, but it's needed so ;]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users