944,121 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 7728
  • C++ RSS
Oct 2nd, 2004
0

How do I pause a Prorgam?

Expand Post »
// 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...
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
dcving is offline Offline
20 posts
since Sep 2004
Oct 2nd, 2004
0

Re: How do I pause a Prorgam?

>#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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  1. char c;
  2.  
  3. while ( cin.get ( c ) && c != '\n' )
  4. ;
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 6th, 2004
0

Re: How do I pause a Prorgam?

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dooda man is offline Offline
15 posts
since May 2004
Oct 6th, 2004
0

Re: How do I pause a Prorgam?

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help Using For Loop To Display Ordered Numbers
Next Thread in C++ Forum Timeline: program to compute that the area and perimeter





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC