4 Replies - 12597 Views - Last Post: 27 March 2009 - 12:40 PM Rate Topic: -----

#1 mixch_s  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 8
  • Joined: 26-March 09

How to exit a program in c++?

Post icon  Posted 27 March 2009 - 11:07 AM

What's the code to exit a programme if i request the user to press any key to exit...

with conditon : You are not allowed to use any other libraries except iostream
Is This A Good Question/Topic? 0
  • +

Replies To: How to exit a program in c++?

#2 Sadaiy  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 107
  • Joined: 03-October 08

Re: How to exit a program in c++?

Posted 27 March 2009 - 11:18 AM

create a program

then a loop

have the loop keep looping until user inputs a value

then end loop

then end program

thats it
Was This Post Helpful? 0
  • +
  • -

#3 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: How to exit a program in c++?

Posted 27 March 2009 - 11:30 AM

Do you mean they must type something, press enter, and then the program ends, or they simply press any key to end the program?

I'm sure there are more ways to do it, but this is very simple and it works. ( system("Pause") is not very good, but my instructors have pretty much required it in my classes)

int main()
{
system("Pause");
return 0;
}



That will simply exit whenever they press a key.

This one requires them to type something before pressing enter, then the program exits:
int main()
{
cout<<"Enter something: ";

getchar();

return 0; //exit now
}

This post has been edited by OliveOyl3471: 27 March 2009 - 11:32 AM

Was This Post Helpful? 0
  • +
  • -

#4 mixch_s  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 8
  • Joined: 26-March 09

Re: How to exit a program in c++?

Posted 27 March 2009 - 12:29 PM

Is it like this?

#include <iostream>
using namespace std;

int main ()
{
	char n;
	
	do
	{ 	cout << "bla bla";
	
		cout << "Press n to exit or any key to continue";
		cin >> answer;
		cout << endl;
	}
	while ( answer != 'n' || answer != 'N' );
	
	if ( answer == 'n' || answer == 'N' )
	{
		cout << "Press a key to continue";
		getchar ();
		return 0;
	}
	return 0;
}


but the complier said no such direction or file

*** MOD EDIT: Added code tags. Please :code: ***

This post has been edited by JackOfAllTrades: 27 March 2009 - 02:00 PM

Was This Post Helpful? 0
  • +
  • -

#5 BlakeJustBlake  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 26
  • View blog
  • Posts: 441
  • Joined: 15-February 09

Re: How to exit a program in c++?

Posted 27 March 2009 - 12:40 PM

first of all, I'm preeeetty sure getchar() is in stdio.h. Try cin.get().
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1