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
How to exit a program in c++?
Page 1 of 14 Replies - 8919 Views - Last Post: 27 March 2009 - 12:40 PM
Replies To: How to exit a program in c++?
#2
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
then a loop
have the loop keep looping until user inputs a value
then end loop
then end program
thats it
#3
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)
That will simply exit whenever they press a key.
This one requires them to type something before pressing enter, then the program exits:
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
#4
Re: How to exit a program in c++?
Posted 27 March 2009 - 12:29 PM
Is it like this?
but the complier said no such direction or file
*** MOD EDIT: Added code tags. Please
***
#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
This post has been edited by JackOfAllTrades: 27 March 2009 - 02:00 PM
#5
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().
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote





|