How do I pause a Prorgam?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2004
Posts: 20
Reputation: dcving is an unknown quantity at this point 
Solved Threads: 0
dcving's Avatar
dcving dcving is offline Offline
Newbie Poster

How do I pause a Prorgam?

 
0
  #1
Oct 2nd, 2004
// Program 2.25: Declares distance of an object
// given speed in MPH and time in hours and minutes
// Author: Eric Martin
// Date: 10/1/2004
#include <string>
#include <iostream>
#include <getch>
using namespace std;
float main ()
{
// Declare speed in MPH
// Declare time in hours and minutes
// Formulate

float speed;
float time;
float distance;
int char;

cout << "Compute distance traveled given speed and time" <<
endl;
cout << "Enter speed in MPH: ";
cin >> speed;
cout << "Enter time in hours and minutes: ";
cin>> time;

distance = speed / time;

cout << "It takes " << time << " minutes to travel "
<< distance << " miles at "
<< speed << " miles per hour" << endl;
int char;
return 0;
}
What do I need to include in this program in order to keep it from closing after getting the result? Someone suggested getch, but the compiler says the file does not exist...
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,596
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 711
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: How do I pause a Prorgam?

 
0
  #2
Oct 2nd, 2004
>#include <getch>
If your compiler supports getch, it will most likely be in conio.h. But because getch is not a standard function, cin.get() is recommended instead:
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. // Any other headers you need
  5.  
  6. int main()
  7. {
  8. // Your program here
  9.  
  10. cin.get();
  11. }
This will work except in cases where cin>> leaves a newline in the stream. This occurs more often than you might think, so it would be a good idea to flush the stream first and then call get:
  1. #include <iostream>
  2. #include <limits>
  3.  
  4. using namespace std;
  5. // Any other headers you need
  6.  
  7. int main()
  8. {
  9. // Your program here
  10.  
  11. cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
  12. cin.get();
  13. }
There are other ways to flush the input stream, most of them are nonstandard. One nifty standard way is to use rdbuf:
  1. cin.ignore ( cin.rdbuf()->in_avail() );
Though whether that's better than numeric_limits is debatable. And there's always the brute force loop:
  1. char c;
  2.  
  3. while ( cin.get ( c ) && c != '\n' )
  4. ;
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 15
Reputation: dooda man is an unknown quantity at this point 
Solved Threads: 0
dooda man dooda man is offline Offline
Newbie Poster

Re: How do I pause a Prorgam?

 
0
  #3
Oct 6th, 2004
to pause a program just put the header conio.h
and when u want to pause write..
system("pause");
and if u want to clear the screen write
system("cls");
hope i've answered ur question
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,596
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 711
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: How do I pause a Prorgam?

 
0
  #4
Oct 6th, 2004
>to pause a program just put the header conio.h
conio.h is not a standard header, and on top of that it isn't the correct header to do what you're suggesting.

>system("pause");
system is declared in stdlib.h. But it's a bad idea in general to use system for things like this because it's not portable (the argument will be different on different systems), it's unsafe ("pause" could be a malicious program), and it's slow because it calls the system command interpreter. The same goes for "cls". Your suggestion if implemented correctly will only work on Windows and DOS machines. You should strive for portability wherever possible, and choose the best nonportable option when you can. system is not the best nonportable option.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC